Advertisement
simov

IspitSeptemvri2014

Jun 20th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. package ispitni;
  2.  
  3. import java.io.*;
  4.  
  5. public class IspitSeptemvri2014 {
  6.  
  7.     public static void main(String[] args) throws IOException {
  8.  
  9.         listanje("in", "out");
  10.  
  11.     }
  12.  
  13.     public static void listanje(String in, String out) throws IOException {
  14.         File sourceFolder = new File(in);
  15.         if (!sourceFolder.exists())
  16.             System.out.println("Ne postoi " + in);
  17.         else {
  18.             if (!sourceFolder.isDirectory()) {
  19.                 System.out.println(in + " ne e direktorium");
  20.                 return;
  21.             }
  22.             File destinationFolder = new File(out);
  23.  
  24.             if (!destinationFolder.exists()) {
  25.                 destinationFolder.mkdirs();
  26.  
  27.             } else {
  28.                 if (destinationFolder.isDirectory())
  29.                     rekurzijaBrisenje(destinationFolder);
  30.                 else {
  31.                     System.out.println(out + " ne e direktorium");
  32.                     return;
  33.                 }
  34.             }
  35.             File tail = new File(destinationFolder, "tail.txt");
  36.  
  37.             tail.createNewFile();
  38.  
  39.             File[] lista = sourceFolder.listFiles();
  40.  
  41.             for (File fajl : lista) {
  42.  
  43.                 if (fajl.isFile()) {
  44.                     System.out.println("Datoteka " + fajl.getName());
  45.  
  46.                     long golemina = fajl.length();
  47.                     float goleminaKb = (float) golemina / 1024;
  48.                     System.out.println("so golemina " + goleminaKb);
  49.  
  50.                     if (fajl.getName().endsWith(".dat")) {
  51.                         fajl.renameTo(new File(destinationFolder, fajl.getName()));
  52.                     }
  53.  
  54.                     // treba da e 160
  55.                     // ama za testiranje pomala brojka
  56.                     // pred submit na reshenieto
  57.                     // da se smeni
  58.                     if (goleminaKb > 0) {
  59.  
  60.                         if (fajl.length() >= 3) {
  61.                             RandomAccessFile vlez = null;
  62.                             RandomAccessFile izlez = null;
  63.                             try {
  64.  
  65.                                 vlez = new RandomAccessFile(fajl, "r");
  66.                                 izlez = new RandomAccessFile(tail, "rw");
  67.  
  68.                                 vlez.seek(vlez.length() - 3);
  69.                                 izlez.seek(izlez.length());
  70.  
  71.                                 byte[] buff = new byte[3];
  72.                                 int offset = 0;
  73.                                 int len = 3;
  74.                                 int read = 0;
  75.  
  76.                                 while (offset != 3) {
  77.                                     read = vlez.read(buff, offset, len);
  78.  
  79.                                     if (read == -1) {
  80.                                         break;
  81.                                     }
  82.  
  83.                                     offset += read;
  84.                                     len = 3 - read;
  85.                                 }
  86.  
  87.                                 izlez.write(buff);
  88.  
  89.                             }
  90.  
  91.                             finally {
  92.                                 if (vlez != null) {
  93.                                     vlez.close();
  94.                                 }
  95.                                 if (izlez != null)
  96.                                     izlez.close();
  97.                             }
  98.                         }
  99.                     }
  100.  
  101.                 } else if (fajl.isDirectory()) {
  102.                     System.out.println("Direktorium " + fajl.getName());
  103.                     listanje(fajl.getName(), out);
  104.  
  105.                 }
  106.             }
  107.  
  108.         }
  109.  
  110.     }
  111.  
  112.     public static void rekurzijaBrisenje(File papka) {
  113.  
  114.         // listanje na direktoriumot papka
  115.         File[] lista = papka.listFiles();
  116.  
  117.         // izminuvanje na site File-ovi
  118.         // od direktoriumot papka
  119.         // koi se smesteni vo lista
  120.  
  121.         for (File f : lista) {
  122.  
  123.             // dokolku e datoteka
  124.             // moze vednas da se izbrise
  125.             if (f.isFile()) {
  126.                 f.delete();
  127.             }
  128.             // dokolku e direktorium
  129.             // vikni ja funkcijata za nego
  130.             // toa kje ovozmozi da se izlista direktoriumot
  131.             // i da se izbrisat site datoteki vo nego
  132.             else if (f.isDirectory()) {
  133.                 rekurzijaBrisenje(f);
  134.                 // otkako kje se izbrisat site datoteki od direktoriumot
  135.                 // direktoriumot kje ostane prazen
  136.                 // i moze bez problem da se izbrise
  137.                 f.delete();
  138.  
  139.             }
  140.         }
  141.  
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement