Java how to get file's disk space like Linux's command "du"?

Dean_Z :

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?

Child Detektiv :

As per answer to question pertaining to Windows ,How do you get the file on disk, and not just the file size in Java?, there is no way to do it by one Java command, you can run Linux du from Java, have a look here for technics How to run Linux commands in Java?.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=141421&siteId=1