Reputation: 1694
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
Upvotes: 1
Views: 115
Reputation: 59416
The difference is because du
will always print the real disk usage while the information stored in this might be larger or smaller.
Sparse files (you can look them up) may store an apparently large file in a small amount of disk storage.
Hard links (you can look them up) may store several files in the place of just one of them.
Block sizes (you can look them up) may waste some bytes at the end of each file and thus files need more storage than they contain.
Upvotes: 0