Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package com.javarush.task.task31.task3101;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. /*
  7. Проход по дереву файлов
  8. */
  9. public class Solution {
  10. public static void main(String[] args) {
  11.  
  12. TreeSet<File> lower50 = new TreeSet<>();
  13.  
  14. File path = new File(args[0]);
  15.  
  16. File resultFileAbsolutePath = new File(args[1]);
  17.  
  18. File allFilesContent = new File(resultFileAbsolutePath.getParent() + "/allFilesContent.txt");
  19. FileUtils.renameFile(resultFileAbsolutePath, allFilesContent);
  20.  
  21. try (FileOutputStream out = new FileOutputStream(allFilesContent)) {
  22.  
  23. Stack stack = new Stack();
  24. stack.push(path);
  25. while (!stack.empty()) {
  26. File elt = (File) stack.pop();
  27. if (elt.isDirectory()) {
  28. for (File file1 : elt.listFiles()) { //pushStack
  29. stack.push(file1);
  30. }
  31. }
  32. if (elt.isFile()) {
  33. if (elt.length() > 50) {
  34. FileUtils.deleteFile(elt);
  35. } else {
  36. lower50.add(elt);
  37. }
  38. }
  39. }
  40.  
  41. TreeMap<String, File> fileAndPath = new TreeMap<>();
  42. for(File f : lower50){
  43. fileAndPath.put(f.getName(), f);
  44. }
  45. for(Map.Entry<String, File> pair : fileAndPath.entrySet()){
  46. BufferedReader bufferedReader = new BufferedReader(new FileReader(pair.getValue()));
  47. String s = "";
  48. while ((s = bufferedReader.readLine()) != null){
  49. out.write((s+"\n").getBytes());
  50. }
  51. bufferedReader.close();
  52. }
  53. out.close();
  54. } catch (IOException e) {
  55. }
  56. }
  57.  
  58. public static void deleteFile(File file) {
  59. if (!file.delete()) System.out.println("Can not delete file with name " + file.getName());
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement