Advertisement
Petrush

[OS] Lab2_3 FileScanner

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