-1

I have a programme that sorts files into sub folders within a main file. I want to be able to print out in the console how many files are in each sub folder. Here is a example of my file structure.

Main file → subfolder → folders I want to count

the subfolder could be any random number so i cannot use perticular file names on this folder. So what I need to do is look in the main file check how many subfolders there are and then get the names of the subfolders. Then go in the subfolders and get a count of how many folders are in there.

I hope you can help, I probably made this sound way harder than it is

Grbe1l
  • 489
  • 4
  • 16
  • You basically states exactly what you need to do yourself, so just start coding. – Roger Gustavsson Jun 01 '15 at 10:40
  • i havent yet found a solution @rogerGustavsson – Grbe1l Jun 01 '15 at 10:40
  • Do you know how to read the contents of a file? – Roger Gustavsson Jun 01 '15 at 10:41
  • yes but dont know how to do both together in this context – Grbe1l Jun 01 '15 at 10:42
  • http://stackoverflow.com/questions/12616124/get-number-of-files-in-a-directory-and-its-subdirectories first hit on google when searching "java count files in directory and subdirectories". Should have enough information. – JohannisK Jun 01 '15 at 10:43
  • If the file contains the name of a subfolder on each line, for each line read from file, look up subfolder on filesystem, scan subfolder for each file. Look into `java.io.*` or `java.nio.file.*` for classes on how to access the filesystem. – Roger Gustavsson Jun 01 '15 at 10:44

1 Answers1

2

Java 7 has a new IO package that supports Visitor pattern on file system tree of directories. Take a look at this tutorial https://docs.oracle.com/javase/tutorial/essential/io/walk.html

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47