Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. package JavaIO;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.io.RandomAccessFile;
  10. import java.util.Random;
  11.  
  12. public class IspitSeptemvri2015termin1 {
  13.  
  14. public static void main(String[] args) throws IOException {
  15. // TODO Auto-generated method stub
  16.  
  17. work("dekemvri","rez");
  18. }
  19.  
  20. public static void rekurzijaBrisenje(File papka) {
  21.  
  22. File[] lista = papka.listFiles();
  23.  
  24. for (File f : lista) {
  25.  
  26. if(f.isFile()){
  27. f.delete();
  28. }
  29.  
  30. else if (f.isDirectory())
  31. {
  32. rekurzijaBrisenje(f);
  33. f.delete();
  34. }
  35. }
  36.  
  37. }
  38.  
  39. public static void work(String in_value, String out_value) throws IOException {
  40.  
  41. File in = new File(in_value);
  42.  
  43. if(!in.exists())
  44. return;
  45. if(!in.isDirectory())
  46. return;
  47.  
  48. File out = new File(out_value);
  49.  
  50.  
  51. if (out.exists()) {
  52. if(out.isDirectory())
  53. rekurzijaBrisenje(out);
  54. else
  55. return;
  56. }
  57. else
  58. out.mkdirs();
  59.  
  60. File rezultat = new File(out,"big.bin");
  61. if(!rezultat.exists())
  62. rezultat.createNewFile();
  63.  
  64. File[] lista = in.listFiles();
  65.  
  66. for (File f : lista) {
  67.  
  68. long golemina = f.length();
  69. float goleminaKB = (float) golemina / 1024;
  70.  
  71. if(f.isFile() && f.getName().endsWith(".txt")) {
  72.  
  73. if (goleminaKB < 10) {
  74. copy(f,out);
  75. }
  76.  
  77. else {
  78. System.out.println(f.getName());
  79.  
  80. File tmp = getRandomFile(in,".bin");
  81.  
  82. tmp.createNewFile();
  83.  
  84. readFromSourceWriteToDestination(rezultat,tmp);
  85.  
  86. rezultat.delete();
  87.  
  88. rezultat.createNewFile();
  89.  
  90. readFromSourceWriteToDestination(f, rezultat);
  91.  
  92. readFromSourceAppendToDestination(tmp,rezultat);
  93. tmp.delete();
  94. }
  95.  
  96. }
  97.  
  98. else if (f.isDirectory())
  99. work(in.getAbsolutePath(),out.getAbsolutePath());
  100.  
  101. }
  102. }
  103.  
  104. private static void readFromSourceAppendToDestination(File source, File destination) throws IOException {
  105. // TODO Auto-generated method stub
  106.  
  107.  
  108. RandomAccessFile vlez = null;
  109. RandomAccessFile izlez = null;
  110.  
  111. try {
  112.  
  113. vlez = new RandomAccessFile(source, "r");
  114.  
  115. izlez = new RandomAccessFile(destination, "rw");
  116.  
  117. izlez.seek(izlez.length());
  118.  
  119. int b = 0;
  120.  
  121. while ((b=vlez.read()) != -1)
  122. izlez.write(b);
  123.  
  124.  
  125.  
  126. }
  127. finally {
  128. if(vlez!=null)
  129. vlez.close();
  130. if(izlez!=null)
  131. izlez.close();
  132. }
  133.  
  134.  
  135. }
  136.  
  137. public static void readFromSourceWriteToDestination (File source,File destination) throws IOException {
  138.  
  139. RandomAccessFile vlez = null;
  140. RandomAccessFile izlez = null;
  141.  
  142. try {
  143.  
  144. vlez = new RandomAccessFile(source, "r");
  145.  
  146. izlez = new RandomAccessFile(destination, "rw");
  147.  
  148. int b = 0;
  149.  
  150. while ((b=vlez.read()) != -1)
  151. izlez.write(b);
  152.  
  153.  
  154.  
  155. }
  156. finally {
  157. if(vlez!=null)
  158. vlez.close();
  159. if(izlez!=null)
  160. izlez.close();
  161. }
  162.  
  163. }
  164.  
  165. public static File getRandomFile(File source, String ekstenzija) throws IOException {
  166. File[] lista = source.listFiles();
  167. String tmp = "tmp";
  168. int randomNumber = getRandomValue(1, 100);
  169. String fileName = tmp + randomNumber + ekstenzija;
  170. while (sourceContainsFileName(source, fileName)) {
  171. randomNumber = getRandomValue(1, 100);
  172. fileName = tmp + randomNumber + ekstenzija;
  173.  
  174. }
  175.  
  176. File tmpFile = new File(source, fileName);
  177. tmpFile.createNewFile();
  178. return tmpFile;
  179.  
  180. }
  181.  
  182. public static int getRandomValue(int min, int max)
  183.  
  184. {
  185. Random r = new Random();
  186. int result = r.nextInt(max - min) + min;
  187. return result;
  188. }
  189.  
  190. public static boolean sourceContainsFileName(File source, String fileName) {
  191. File[] lista = source.listFiles();
  192. for (File f : lista) {
  193. if (f.getName().equals(fileName))
  194. return true;
  195.  
  196. }
  197. return false;
  198.  
  199. }
  200.  
  201.  
  202. public static void copy(File vleznaDatoteka, File destination) throws IOException {
  203. // TODO Auto-generated method stub
  204.  
  205. File nov = new File(destination, vleznaDatoteka.getName());
  206.  
  207. if(nov.exists()){
  208. System.out.println("datotekata " + vleznaDatoteka.getName() + " vekje postoi");
  209.  
  210. } else {
  211. nov.createNewFile();
  212.  
  213. BufferedReader vlez = null;
  214. BufferedWriter izlez = null;
  215.  
  216. try {
  217. vlez = new BufferedReader(new FileReader(vleznaDatoteka));
  218. izlez = new BufferedWriter(new FileWriter(nov));
  219.  
  220. String line;
  221.  
  222. while((line = vlez.readLine()) != null)
  223. izlez.write(line + "\r\n");
  224. }
  225. finally {
  226. if(vlez!=null)
  227. vlez.close();
  228. if(izlez!=null)
  229. izlez.close();
  230. }
  231. }
  232. }
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement