Calculate file or folder disk space with Java
Last Updated on Thursday, 07 June 2012 15:38
Compute disk usage of a file or Folder
Compute disk usage of a file or folder using Java io.
Writing your own utility class
The following snippet give you a simple and effective way to calculate the disk space used by a file or folder using a FileFilter visitor.
To compute and display the file size you have to give the file or folder as an instance to visit to your DiskUsage class then you can display the result using the getSize() method;
Using Apache Commons IO
If you have apache commons io in your classpath you can use FileUtils.sizeOfDirectory(folder) for folder or sizeOf(file) for a file.Tags: java, class, public, folder, visitor, file, long, calculate, java.io.filefilter;, java.io.file;import, getsize(), filefilter, disk