Advertisement
Azure47

OS Lab 2.3

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