Why used space calculated, for a directory, using java and 'du -sk' are different ?, also what is the exact java alternative for 'du -sk'?
PFB Java Code,
final String location = "/home/bhlabhla";
final Path path = Paths.get(location);
final Process process = Runtime.getRuntime().exec("du -sk " + location);
final BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
System.out.println("used space [du -sk]");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("***************************");
// 1
System.out.println("used space [Recursion]:" + getFolderSize(new File(location)) / 1024);
// 2
final long size = Files.walk(path).mapToLong(p -> p.toFile().length()).sum();
System.out.println("used space [Java 8]:" + size / 1024);
public static long getFolderSize(final File dir) {
long size = 0;
for (final File file : dir.listFiles()) {
if (file.isFile()) {
size += file.length();
} else {
size += getFolderSize(file);
}
}
return size;
}
Out put,
used space [du -sk]
83164000 /home/bhlabhla
used space [Recursion]:83151664
used space [Java 8]:83153560