How to find how much disk space is left using Java?
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().
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
Use CommonsIO and FilesystemUtils:
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();