I wrote recursive method to calculate folder size:
private static long calcSize(File dir) {
if (dir.isFile() && dir.canRead()) {
return dir.length();
}
long size = 0;
if ( dir.exists() && dir.isDirectory() && dir.canRead()) {
for (File file : dir.listFiles()) { //Here NPE
if (file.isFile() && dir.canRead())
size += file.length();
else if (file.isDirectory())
size += calcSize(file);
else
throw new Error("What is this: " + file);
}
}
return size;
}
added additional checks as users advised. still getting NPE.
NPE occurs when executing:
calcSize(new File("D:/"))
on another folders it works fine. but on D:/ and C:/ i get the exception... Maybe its because i have hidden system directories on which i have no access rights? Your help would be appreciated.