Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Folder Size in java  RSS feed

 
kirba devi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Could somebody help me in fixing this problem.
Problem-How to get folder size in java?

For eg: Test folder
new File("D:\\Test").

Regards
Kirba.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you've probably found, File's length() method returns an "unspecified" value if the File object represents a directory.

So I think you need to iterate through the contents of the directory, and for each element that is a file, get the length and add it to a cumulative counter field. For each element that is a directory (i.e., a subdirectory), you will need to repeat this process (using recursion) until you get down to the final level.

See File methods listFiles(), isFile(), and isDirectory().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!