• Post Reply Bookmark Topic Watch Topic
  • New Topic

Determining Folder/File Size

 
kaushik selvaraj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am developing a management site where the system admin can have a look at the different input files, their sizes and the total folder size, free space etc. Is there any java api or any open source stuff that would help in getting this data? My application needs to run on both windows and unix machines so i want to have a pure java based solution.
Thanks
Kaushik
 
Marie Mazerolle
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class java.io.File has a length() method that returns the file size.
 
kaushik selvaraj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tip marie. It workd fine for files but returns 0 for directories. So how do we find out the directory size? Is iterating thru all files and adding the lengths the only solution?
Kaushik
 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, yes....The size of the directory will always be zero. And the only way that I know of the get the size of full contents of the directory would be to iterate through them. Just create a recursive method that will drill down the dirs and total the bytes.
Here is quick one that I created...Do what you will with the Exceptions.
Good Luck!
 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! Ignore the else-if...I forgot to remove that from additional code.
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, on Linux/Unix system, beware of soft links (ln -s xxx xxx). In that case, I think you need to get absolute path to check it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!