I am using java.io.File.length() method to get size of a file.
But for a sparse file this value can be very different from actual disk space used by that file.
Is there a way to get actual disk usage for the file?
The apparent size of a file is the number of bytes in a file. For example, a file containing the word ‘zoo’ with no newline would, of course, have an apparent size of 3. However, a sparse file created with this command:
dd bs=1 seek=2GiB if=/dev/null of=big
has an apparent size of 2 GiB, yet on most modern systems, it actually uses almost no disk space.