• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to print directories and the number of files inside them given the folder path  RSS feed

 
Qubayel Alqhtani
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Experts, I'm trying to take the path of a folder from the user then print the directories name and the number of files inside them as a table in txt file
for example I have a folder called TestFolder inside this folder there are 2 files one is called directory1 and the other is called directory2
inside directory1  there are 5 text files and inside directory2  there are 3 text file

the function should return something like this

directory1 : 5 files
directory2 : 3 files

hope you got the point this is my code and I'm really stuck
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the newer Files API instead of File. If yoiu look through that link, you will find a section about creating and checking directories. You can create a DirectoryStream, but you may have to use a loop to count its contents.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not too far off. Instead of
I would suggest
I think you'll find that it makes the recursion slightly simpler, line 16 would no longer need getAbsolutePath().

Make "counter" a local variable. That way each level of recursion gets its own copy.

For your loop use an enhanced for() loop.

Also
should be written
This is the Java convention.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!