tags:

views:

594

answers:

3

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

+4  A: 

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().

Chris Klepeis
+1 good memory I guess.
kd304
+15  A: 

Just look at the documentation. This is one of the new features in 1.6.

These new methods also include:

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


If your still using 1.5 then you can use the Apache Commons library and it's FileSystem class

Lucas McCoy
+6  A: 

Use CommonsIO and FilesystemUtils:

http://commons.apache.org/io/api-release/org/apache/commons/io/FileSystemUtils.html#freeSpaceKb(java.lang.String)

e.g.

FileSystemUtils.freeSpaceKb("/");

or built into the JDK:

http://java.sun.com/javase/6/docs/api/java/io/File.html#getFreeSpace()

new File("/").getFreeSpace();
Jon