Advertisement
simov

IspitJanuari2015

Jun 20th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. /*
  2. Сите txt датотеки кои се поголеми од 10 KB од именикот in
  3. да се запишат во rezultat.txt (ако не постои да се креира)
  4. така што секоја датотека ќе биде запишана
  5. на почетокот на rezultat.txt.
  6. Значи на крај содржината на rezultat.txt треба да биде
  7. таква што последната датотека ќе биде запишана на почетокот,
  8. претпоследната под неа итн ...
  9. Ако има датотеки со содржина A, B, C,
  10. резултатот треба да е
  11. C B A
  12. */
  13.  
  14. package ispitni;
  15.  
  16. import java.io.*;
  17. import java.util.List;
  18. import java.util.Random;
  19.  
  20. public class IspitJanuari2015 {
  21.  
  22.     public static void main(String[] args) throws IOException {
  23.         // TODO Auto-generated method stub
  24.  
  25.         File papka = new File("dekemvri");
  26.         File rezultat = new File(papka, "rezultat.txt");
  27.         rezultat.createNewFile();
  28.  
  29.         func(papka, rezultat);
  30.  
  31.     }
  32.  
  33.     public static void func(File source, File rezultat) throws IOException {
  34.  
  35.         if (source.isDirectory()) {
  36.             File[] lista = source.listFiles();
  37.  
  38.             for (File f : lista) {
  39.  
  40.                 long golemina = f.length();
  41.                 float goleminaKB = (float) golemina / 1024;
  42.  
  43.                 // samo za testiranje, treba da e 10
  44.                 if (goleminaKB > 0 && f.isFile()) {
  45.                     // System.out.println(f.getName());
  46.                     File tmp = getRandomFile(source, ".txt");
  47.                     readFromSourceWriteToDestination(rezultat, tmp);
  48.  
  49.                     rezultat.delete();
  50.                     rezultat.createNewFile();
  51.                     readFromSourceWriteToDestination(f, rezultat);
  52.  
  53.                     readFromSourceAppendToDestination(tmp, rezultat);
  54.                     tmp.delete();
  55.  
  56.                 }
  57.  
  58.             }
  59.         }
  60.     }
  61.  
  62.     public static void readFromSourceWriteToDestination(File source, File destination) throws IOException {
  63.         RandomAccessFile vlez = null;
  64.         RandomAccessFile izlez = null;
  65.         try {
  66.             vlez = new RandomAccessFile(source, "r");
  67.             izlez = new RandomAccessFile(destination, "rw");
  68.  
  69.             int b;
  70.  
  71.             while ((b = vlez.read()) != -1)
  72.                 izlez.write(b);
  73.  
  74.         } finally {
  75.             if (izlez != null) {
  76.                 izlez.close();
  77.             }
  78.             if (vlez != null) {
  79.                 vlez.close();
  80.             }
  81.         }
  82.  
  83.     }
  84.  
  85.     public static void readFromSourceAppendToDestination(File source, File destination) throws IOException {
  86.         RandomAccessFile vlez = null;
  87.         RandomAccessFile izlez = null;
  88.         try {
  89.             vlez = new RandomAccessFile(source, "r");
  90.             izlez = new RandomAccessFile(destination, "rw");
  91.  
  92.             izlez.seek(izlez.length());
  93.  
  94.             int b;
  95.  
  96.             while ((b = vlez.read()) != -1)
  97.                 izlez.write(b);
  98.  
  99.         } finally {
  100.             if (izlez != null) {
  101.                 izlez.close();
  102.             }
  103.             if (vlez != null) {
  104.                 vlez.close();
  105.             }
  106.         }
  107.  
  108.     }
  109.  
  110.     public static File getRandomFile(File source, String ekstenzija) throws IOException {
  111.         File[] lista = source.listFiles();
  112.         String tmp = "tmp";
  113.         int randomNumber = getRandomValue(1, 100);
  114.         String fileName = tmp + randomNumber + ekstenzija;
  115.         while (sourceContainsFileName(source, fileName)) {
  116.             randomNumber = getRandomValue(1, 100);
  117.             fileName = tmp + randomNumber + ekstenzija;
  118.  
  119.         }
  120.  
  121.         File tmpFile = new File(source, fileName);
  122.         tmpFile.createNewFile();
  123.         return tmpFile;
  124.  
  125.     }
  126.  
  127.     public static int getRandomValue(int min, int max)
  128.  
  129.     {
  130.         Random r = new Random();
  131.         int result = r.nextInt(max - min) + min;
  132.         return result;
  133.     }
  134.  
  135.     public static boolean sourceContainsFileName(File source, String fileName) {
  136.         File[] lista = source.listFiles();
  137.         for (File f : lista) {
  138.             if (f.getName().equals(fileName))
  139.                 return true;
  140.  
  141.         }
  142.         return false;
  143.  
  144.     }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement