简体   繁体   English

Java如何获取文件的磁盘空间,如Linux的命令“du”?

[英]Java how to get file's disk space like Linux's command “du”?

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". 我知道Java可以使用File.length(), FileChannel.size()RandomAccessFile.length()来获取文件的长度,但这等于Linux的命令“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. 并使用“ls -l filePath”,我得到文件的长度是10485760,但使用“du filePath”只得到0。

So, how to use Java to get the file's real used disk space? 那么,如何使用Java来获取文件的实际使用磁盘空间呢?

As per answer to question pertaining to Windows , How do you get the file on disk, and not just the file size in Java? 根据有关Windows的问题的答案, 如何在磁盘上获取文件,而不仅仅是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? ,没有办法通过一个Java命令来做,你可以从Java运行Linux du ,看看这里的技术如何在Java中运行Linux命令? .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM