wood burning stoves
The moose likes Beginning Java and the fly likes Getting A Directory Or File Size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting A Directory Or File Size" Watch "Getting A Directory Or File Size" New topic
Author

Getting A Directory Or File Size

Jonathan Fleming
Greenhorn

Joined: Jul 18, 2003
Posts: 2
How to you get a directory or file size into an instance variable?
Thanks
Jonathan :^)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Look at the java.io.File class. You can create an instance with a filename arg and then check the length. I don't know what it will do for a directory.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
On my Windows XP machine, the length of a directory is 0.
To get the total size of all files in a directory, take a look at that File.listFiles() method.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Rats, you may have to use recursion to the ends of the tree to get the size of all files in a directory. I do this in REXX and make a CSV with these columns:
directory
#files in this directory
#bytes in those files
#files in this plus all subdirectories
#bytes in those files
It's an interesting way to explore disk usage. There are a number of fascinating ways to graph this kinda thing. There is much fun to be had if you want to follow it up.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Getting A Directory Or File Size
 
It's not a secret anymore!