aktuelle.kurse/oldies/m411/docs/Daten-Uebungen-CodeBeispiele/Rekursion/DiskUsage.java

28 lines
566 B
Java
Raw Normal View History

2021-08-06 18:08:13 +02:00
package recusion;
import java.io.File;
public class DiskUsage {
public static void main(String[] args) {
long size = diskUsage(new File("/users/capa/tmp"));
System.out.println("Total size is [" + size + "] Bytes.");
}
public static long diskUsage(File f) {
long diskUsage = 0;
if (f.isFile()) { diskUsage = f.length(); }
System.out.println("Checking " + f);
if (f.isDirectory()) {
File[] fileList = f.listFiles();
if (fileList != null) {
for (File g : fileList) {
diskUsage += diskUsage(g);
}
}
}
return diskUsage;
}
}