File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Size of File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Size of File" Watch "Size of File" New topic

Size of File

Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
Dear all,
How can I get the size of a folder ?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I'm not sure if this is exactly what you're looking for, but I was able to come up with the size of all the files within a directory by using a recursive function, like this:

I'm not really sure if there is an easier way to do this or not, but it seems that simply calling the length method on a File object that represents a directory simply returns 0. Therefore, I had to look at each file individually and recurse through any subdirectories. The final result printed the size of all of the files contained within the directory C:\test in bytes.
I hope this helps you out.

SCJP Tipline, etc.
I agree. Here's the link:
subject: Size of File
It's not a secret anymore!