Seeking a recursive folder size (two)


Test1 {class public
public static void main (String [] args) {
// folder size statistics
Long len = getDirLength (new new File ( "D: \\ the JavaSE"));
System.out.println (+ len "bytes ");
}
// return value type long, a list of parameters: the dir File
public static long getDirLength (File the dir) {
// definition of statistical variables
long len = 0;

// get all sub-content directory
File [] files = dir .listFiles ();
// determine whether the empty
IF (files = null!) {
for (file file: files) {
// If the file, the cumulative size of the file (recursively export)
IF (file.isFile ()) {
file.length + = len ();
} the else {
// If the folder, recursively calling
len + = getDirLength (file); // Do not forget the size of the accumulated subfolders
}
}
}
return len;
}
}

Guess you like

Origin www.cnblogs.com/robotsu/p/11525405.html