deyanmalinov

8. Nested Folders

Apr 8th, 2020
211
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DPM;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayDeque;
  6. import java.util.Deque;
  7.  
  8. public class Main {
  9.     public static void main(String[] args) throws IOException {
  10.         String path = "D:\\Coding\\Java\\Softuni\\Java Advanced - January 2019\\" +
  11.                 "04. Java-Advanced-Fiels-and-Directories-Lab\\04. " +
  12.                 "Java-Advanced-Files-and-Streams-Lab-Resources\\Files-and-Streams";
  13.         File root = new File(path);
  14.  
  15.         Deque<File> dirs = new ArrayDeque<>();
  16.         dirs.offer(root);
  17.         int count = 0;
  18.         while (!dirs.isEmpty()) {
  19.             File current = dirs.poll();
  20.             File[] nestedFiles = current.listFiles();
  21.             for (File nestedFile : nestedFiles)
  22.                 if (nestedFile.isDirectory())
  23.                     dirs.offer(nestedFile);
  24.             count++;
  25.             System.out.println(current.getName());
  26.         }
  27.         System.out.println(count + " folders");
  28.     }
  29. }
RAW Paste Data