Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- public class FileScanner extends Thread{
- private String fileToScan;
- //TODO: Initialize the start value of the counter
- private static Long counter = (long)0;
- public FileScanner (String fileToScan) {
- this.fileToScan=fileToScan;
- //TODO: Increment the counter on every creation of FileScanner object
- counter++;
- }
- public static void printInfo(File file) {
- /* TODO: Print the info for the @argument File file, according to the requirement of the task */
- System.out.println("file: " + file.getAbsolutePath() + " " + file.length());
- }
- public static Long getCounter () {
- return counter;
- }
- public void run() {
- //TODO Create object File with the absolute path fileToScan.
- File file = new File(fileToScan);
- //TODO Create a list of all the files that are in the directory file.
- File [] files = file.listFiles();
- if(file.length() != 0) {
- for (File f : files) {
- /*
- * TODO If the File f is not a directory, print its info using the function printInfo(f)
- * */
- if (!f.isDirectory()) {
- printInfo(f);
- }
- /*
- * TODO If the File f is a directory, create a thread from type FileScanner and start it.
- * */
- Thread asd = new FileScanner(f.getAbsolutePath());
- if (f.isDirectory()) {
- System.out.println("dir: " + f.getAbsolutePath() + " " + f.length());
- asd.start();
- }
- //TODO: wait for all the FileScanner-s to finish
- try {
- asd.join();
- } catch (InterruptedException e) {
- }
- }
- }
- }
- public static void main (String [] args) {
- String FILE_TO_SCAN = "C:/Users/189075/Desktop/lab";
- //TODO Construct a FileScanner object with the fileToScan = FILE_TO_SCAN
- FileScanner fileScanner = new FileScanner(FILE_TO_SCAN);
- //TODO Start the thread from type FileScanner
- fileScanner.start();
- //TODO wait for the fileScanner to finish
- try {
- fileScanner.join();
- }
- catch (InterruptedException e){}
- //TODO print a message that displays the number of thread that were created
- System.out.println(counter + " nitki bea kreirani");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement