kristina7

Прв колоквиум(Група 1) - ОС

Mar 11th, 2017
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;
  5. import java.util.List;
  6.  
  7. public class PrvKolokvium_Grupa1 {
  8.     public  void moveWritableTxtFiles(String from, String to){
  9.         File f = new File(from);
  10.         File t = new File(to);
  11.        
  12.         if(!f.exists()){
  13.             System.out.println("Ne postoi");
  14.             return;
  15.         }
  16.         if(!t.exists()){
  17.             t.mkdirs();
  18.         }
  19.        
  20.         File[] files = f.listFiles();
  21.         for (File file : files) {
  22.             if(file.getName().endsWith(".txt") && file.canWrite()){
  23.                 File newFile = new File(to, file.getName());
  24.                 file.renameTo(newFile);
  25.             }
  26.         }
  27.        
  28.     }
  29.     public void deserializeData(String source, List<byte[]> data, long elementLength) throws IOException{
  30.         FileInputStream fis = null;
  31.         try {
  32.             fis = new FileInputStream(source);
  33.             byte[] element = new byte[(int) elementLength];
  34.             int b;
  35.             int counter =0;
  36.             while((b = fis.read()) == -1){
  37.                 element[counter] = (byte) b;
  38.                 counter++;
  39.                 if(counter == elementLength){
  40.                     data.add(element);
  41.                     counter = 0;
  42.                     element = new byte[ (int) elementLength];
  43.                 }
  44.             }          
  45.         } catch (Exception e) {
  46.             // TODO: handle exception
  47.         } finally{
  48.             if(fis != null)
  49.                 fis.close();
  50.         }
  51.     }
  52.     public static void invertLargeFile(String source, String destination) throws IOException{
  53.        
  54.         RandomAccessFile rr = null;
  55.         RandomAccessFile rw = null;
  56.         try {
  57.             rr = new RandomAccessFile(source, "r");
  58.             rw = new RandomAccessFile(destination, "rw");
  59.             long pos = rr.length() - 1;
  60.            
  61.             while(pos >= 0){
  62.                 rw.seek(pos);
  63.                 char c = (char) rr.read();
  64.                 rw.write(c);
  65.                 pos -= 1;
  66.             }
  67.            
  68.         } catch (Exception e) {
  69.             // TODO: handle exception
  70.         }finally{
  71.             if(rr != null){
  72.                 rr.close();
  73.             }
  74.             if(rw != null){
  75.                 rw.close();
  76.             }
  77.         }
  78.        
  79.     }
  80.     public static void main(String[] args) throws IOException {
  81.        
  82.         File s = new File("source.txt");
  83.         File d = new File("destinacija.txt");
  84.        
  85.         invertLargeFile(s.getName(), d.getName());
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment