I know Java can use File.length(), FileChannel.size()
or RandomAccessFile.length()
to get a file's length, but this equals Linux's command "ls". However, when I create a file with a specific length as follows:
RandomAccessFile randomAccessFile = new RandomAccessFile(filePath,"rw");
randomAccessFile.setLength(10485760);
and use "ls -l filePath", I get the file's length is 10485760, but using "du filePath" only gets 0.
So, how to use Java to get the file's real used disk space?