Just go through all files and sum the length of them:
/**
* Return the size of a directory in bytes
*/
private static long dirSize(File dir) {
if (dir.exists()) {
long result = 0;
File[] fileList = dir.listFiles();
if (fileList != null) {
for(int i = 0; i < fileList.length; i++) {
// Recursive call if it's a directory
if(fileList[i].isDirectory()) {
result += dirSize(fileList[i]);
} else {
// Sum the file size in bytes
result += fileList[i].length();
}
}
}
return result; // return the file size
}
return 0;
}
NOTE: Function written by hand so it could not compile!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…