Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.util.List;
- public class PrvKolokvium_Grupa1 {
- public void moveWritableTxtFiles(String from, String to){
- File f = new File(from);
- File t = new File(to);
- if(!f.exists()){
- System.out.println("Ne postoi");
- return;
- }
- if(!t.exists()){
- t.mkdirs();
- }
- File[] files = f.listFiles();
- for (File file : files) {
- if(file.getName().endsWith(".txt") && file.canWrite()){
- File newFile = new File(to, file.getName());
- file.renameTo(newFile);
- }
- }
- }
- public void deserializeData(String source, List<byte[]> data, long elementLength) throws IOException{
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(source);
- byte[] element = new byte[(int) elementLength];
- int b;
- int counter =0;
- while((b = fis.read()) == -1){
- element[counter] = (byte) b;
- counter++;
- if(counter == elementLength){
- data.add(element);
- counter = 0;
- element = new byte[ (int) elementLength];
- }
- }
- } catch (Exception e) {
- // TODO: handle exception
- } finally{
- if(fis != null)
- fis.close();
- }
- }
- public static void invertLargeFile(String source, String destination) throws IOException{
- RandomAccessFile rr = null;
- RandomAccessFile rw = null;
- try {
- rr = new RandomAccessFile(source, "r");
- rw = new RandomAccessFile(destination, "rw");
- long pos = rr.length() - 1;
- while(pos >= 0){
- rw.seek(pos);
- char c = (char) rr.read();
- rw.write(c);
- pos -= 1;
- }
- } catch (Exception e) {
- // TODO: handle exception
- }finally{
- if(rr != null){
- rr.close();
- }
- if(rw != null){
- rw.close();
- }
- }
- }
- public static void main(String[] args) throws IOException {
- File s = new File("source.txt");
- File d = new File("destinacija.txt");
- invertLargeFile(s.getName(), d.getName());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment