1

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

Devas
  • 1,544
  • 4
  • 23
  • 28
  • 2
    The answer to the first queston is file system overhead, in a couple of different ways, the largest typically being cluster size. A 789 byte file doesn't use exactly 789 bytes of storage. It uses some multiple of the minimum storage unit, frequently 4k or even 16k. I don't know the answer to the question question, though. – T.J. Crowder Aug 09 '16 at 09:43
  • Thank you for the reply. If that's the case, is it possible to get the file system overhead through a java api. – Devas Aug 09 '16 at 10:20
  • 1
    There is no such Java API. – user207421 Aug 09 '16 at 11:18

1 Answers1

0

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.

Alfe
  • 56,346
  • 20
  • 107
  • 159