77

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

Roman Kagan
  • 10,440
  • 26
  • 86
  • 126

4 Answers4

107

Have a look at the File class 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 you're still using 1.5 then you can use the Apache Commons IO library and its FileSystem class

Kredns
  • 36,461
  • 52
  • 152
  • 203
  • 28
    Note very importantly that getUsableSpace is not equal to getFreeSpace. On Linux file systems for example, partitions very often have a number of reserved blocks. These are included in the value returned by getFreeSpace() but not in the value from getUsableSpace(). So if you are interested in how much space you have to write files in, use getUsableSpace - NOT getFreeSpace(). – Mikkel Jul 24 '14 at 17:11
  • Those were golden days during my college. I am reading this comment now and thinking how confident we were on whatever little we knew !!! – Surajit Biswas Jun 12 '23 at 07:36
72

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

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.

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
prunge
  • 22,460
  • 3
  • 73
  • 80
  • 2
    On Linux Debian the FileSystems.getDefault().getRootDirectories() call only returns the "/" directory. – pako Sep 10 '16 at 19:29
  • @pako There is only one root on unix/linux: `/`. There are multiple roots only on windows, one for each drive letter. – pieroxy Jan 15 '21 at 20:16
25

Use CommonsIO and FilesystemUtils:

https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileSystemUtils.html#freeSpaceKb()

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();
Freiheit
  • 8,408
  • 6
  • 59
  • 101
Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150
3

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

  • getTotalSpace()
  • getFreeSpace()

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.

  • 1
    Welcome to Stack Overflow! Please note that bare links to your own website/product are not encouraged here for two reasons; First, an answer should be posted as a self-contained answer, not a mere link to an external site. Second, self-promotion tends to be frowned upon here, and often is flagged as spam (especially if there is no disclosure that you are linking to your own site/product). – Andrew Barber Apr 02 '13 at 17:12