Tue Oct 21, 2008 7:52 pm
Tue Oct 21, 2008 7:53 pm
import java.io.File;
public class What033 {
public static void main(String[] args) {
What033 w = new What033();
File dir = new File("D:\\temp");
System.out.println(w.getDirSizeInMegabytes(dir));
}
long getDirSize(File dir) {
long size = 0;
if (dir.isFile()) {
size = dir.length();
} else {
File[] subFiles = dir.listFiles();
for (File file : subFiles) {
if (file.isFile()) {
size += file.length();
} else {
size += this.getDirSize(file);
}
}
}
return size;
}
long getDirSizeInMegabytes(File dir) {
return this.getDirSize(dir) / 1024 / 1024;
}
}
Tue Mar 17, 2009 7:13 pm
http://commons.apache.org/io/api-release/index.html
Thu Jun 07, 2012 4:31 pm
import java.io.File;
import java.io.FileFilter;
public class DiskUsage implements FileFilter
{
public DiskUsage(){};
private long size = 0;
public boolean accept(File file)
{
if ( file.isFile())
size += file.length();
else
file.listFiles(this);
return false;
}
public long getSize()
{
return size;
}
}
The complete tutorial is available at http://www.java-tutorial.ch/core-java-tutorial/calculate-file-disk-space/usage-with-java
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.