Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. import java.io.*;
  2. import java.io.File;
  3. import java.util.HashSet;
  4.  
  5.  
  6. public class FileScanner extends Thread {
  7.  
  8. private String fileToScan;
  9. //TODO: Initialize the start value of the counter
  10. private static Long counter = 0L;
  11.  
  12. public FileScanner (String fileToScan) {
  13. this.fileToScan=fileToScan;
  14. //TODO: Increment the counter on every creation of FileScanner object
  15. synchronized (this) {
  16. counter++;
  17. }
  18.  
  19. }
  20.  
  21. public static long getDirectorySize(File f){
  22. if(!f.isDirectory())
  23. return 0;
  24. long length = 0;
  25.  
  26. for(File file : f.listFiles()){
  27. if(file.isFile())
  28. length+=file.length();
  29. else if(file.isDirectory())
  30. length+=getDirectorySize(file);
  31. }
  32. return length;
  33. }
  34.  
  35. public static void printInfo(File file) {
  36.  
  37. /*
  38. * TODO: Print the info for the @argument File file, according to the requirement of the task
  39. * */
  40.  
  41.  
  42. if(file.isDirectory()){
  43. long size = getDirectorySize(file);
  44. System.out.println("dir: "+ file.getAbsolutePath()+" "+size);
  45. }
  46. else if(file.isFile())
  47. System.out.println("file: "+ file.getAbsolutePath()+" "+file.length());
  48.  
  49. }
  50.  
  51. public static Long getCounter () {
  52. return counter;
  53. }
  54.  
  55.  
  56. public void run() {
  57.  
  58. //TODO Create object File with the absolute path fileToScan.
  59. File file = new File(fileToScan);
  60.  
  61. //TODO Create a list of all the files that are in the directory file.
  62. File [] files =file.listFiles();
  63.  
  64.  
  65. HashSet<Thread> threads = new HashSet<>();
  66.  
  67. for (File f : files) {
  68. /*
  69. * TODO If the File f is not a directory, print its info using the function printInfo(f)
  70. * */
  71. if(!f.isDirectory())
  72. printInfo(f);
  73. /*
  74. * TODO If the File f is a directory, create a thread from type FileScanner and start it.
  75. * */
  76. else {
  77. Thread thread= new FileScanner(f.getAbsolutePath());
  78. threads.add(thread);
  79. thread.start();
  80.  
  81.  
  82.  
  83. }
  84. //TODO: wait for all the FileScanner-s to finish
  85. for(Thread t : threads){
  86. try{
  87. t.join();
  88. }catch(InterruptedException e){
  89. e.getStackTrace();
  90. }
  91. }
  92. }
  93.  
  94. }
  95.  
  96. public static void main (String [] args) throws InterruptedException {
  97. String FILE_TO_SCAN = "D:/OS-Projects/";
  98.  
  99. //TODO Construct a FileScanner object with the fileToScan = FILE_TO_SCAN
  100. FileScanner fileScanner = new FileScanner(FILE_TO_SCAN);
  101.  
  102. //TODO Start the thread from type FileScanner
  103. fileScanner.start();
  104. //TODO wait for the fileScanner to finish
  105. fileScanner.join();
  106. //TODO print a message that displays the number of thread that were created
  107. System.out.println(getCounter());
  108.  
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement