如何使用Java查找剩余的磁盘空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1051295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:46:46  来源:igfitidea点击:

How to find how much disk space is left using Java?

javadiskspace

提问by Roman Kagan

How to find how much disk space is left using Java?

如何使用Java查找剩余的磁盘空间?

采纳答案by Kredns

Have a look at the File class documentation. This is one of the new features in 1.6.

查看 File 类文档。这是 1.6 中的新功能之一。

These new methods also include:

这些新方法还包括:

  • public long getTotalSpace()
  • public long getFreeSpace()
  • public long getUsableSpace()
  • public long getTotalSpace()
  • public long getFreeSpace()
  • public long getUsableSpace()


If you're still using 1.5 then you can use the Apache Commons IOlibrary and its FileSystem class

如果您仍在使用 1.5,那么您可以使用Apache Commons IO库及其FileSystem 类

回答by Chris Klepeis

Link

关联

If you are a Java programmer, you may already have been asked this simple, stupide question: “how to find the free disk space left on my system?”. The problem is that the answer is system dependent. Actually, it is the implementation that is system dependent. And until very recently, there was no unique solution to answer this question, although the need has been logged in Sun's Bug Database since June 1997. Now it is possible to get the free disk space in Java 6 with a method in the class File, which returns the number of unallocated bytes in the partition named by the abstract path name. But you might be interested in the usable disk space (the one that is writable). It is even possible to get the total disk space of a partition with the method getTotalSpace().

如果您是一名 Java 程序员,您可能已经被问到这个简单而愚蠢的问题:“如何找到系统上剩余的可用磁盘空间?”。问题是答案取决于系统。实际上,它是依赖于系统的实现。直到最近,还没有唯一的解决方案来回答这个问题,尽管自 1997 年 6 月以来就已经在 Sun 的 Bug 数据库中记录了这一需求。现在可以使用类 File 中的方法在 Java 6 中获取可用磁盘空间,它返回由抽象路径名命名的分区中未分配的字节数。但是您可能对可用磁盘空间(可写的磁盘空间)感兴趣。甚至可以使用 getTotalSpace() 方法获取分区的总磁盘空间。

回答by prunge

Java 1.7 has a slightly different API, free space can be queried through the FileStoreclass through the getTotalSpace(), getUnallocatedSpace()and getUsableSpace()methods.

Java 1.7 有一个稍微不同的 API,可以通过FileStore类通过getTotalSpace()getUnallocatedSpace()getUsableSpace()方法查询可用空间。

NumberFormat nf = NumberFormat.getNumberInstance();
for (Path root : FileSystems.getDefault().getRootDirectories()) {

    System.out.print(root + ": ");
    try {
        FileStore store = Files.getFileStore(root);
        System.out.println("available=" + nf.format(store.getUsableSpace())
                            + ", total=" + nf.format(store.getTotalSpace()));
    } catch (IOException e) {
        System.out.println("error querying space: " + e.toString());
    }
}

The advantage of this API is that you get meaningful exceptions back when querying disk space fails.

这个 API 的优点是当查询磁盘空间失败时你会得到有意义的异常。

回答by user2237049

in checking the diskspace using java you have the following method in java.io File class

在使用 java 检查磁盘空间时,您在 java.io File 类中有以下方法

  • getTotalSpace()
  • getFreeSpace()
  • getTotalSpace()
  • 获取自由空间()

which will definitely help you in getting the required information. For example you can refer to http://javatutorialhq.com/java/example-source-code/io/file/check-disk-space-java/which gives a concrete example in using these methods.

这肯定会帮助您获得所需的信息。例如,您可以参考http://javatutorialhq.com/java/example-source-code/io/file/check-disk-space-java/,它给出了使用这些方法的具体示例。

回答by Exodus

public class MemoryStatus{  
 public static void main(String args[])throws Exception{  
  Runtime r=Runtime.getRuntime();  
  System.out.println("Total Memory: "+r.totalMemory());  
  System.out.println("Free Memory: "+r.freeMemory());    
 }
}

Try this code to get diskspace

试试这个代码来获取磁盘空间