How to find how much disk space is left using Java?
4 Answers
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

- 36,461
- 52
- 152
- 203
-
28Note 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
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.

- 5,001
- 11
- 47
- 72

- 22,460
- 3
- 73
- 80
-
2On 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
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();

- 8,408
- 6
- 59
- 101

- 62,090
- 32
- 125
- 150
-
Is there any other built-in function that serves the purpose without the need to create a new file? – user892871 Dec 20 '12 at 04:55
-
5It doesn't create a new file on disk, just a File object, if that's what you are worried about – Jerico Sandhorn Jan 22 '14 at 22:31
-
1`FileSystemUtils` just execs the "df" command (or uses DIR on windows). pretty terrible. – Jeffrey Blattman Jan 07 '16 at 00:04
-
FYI - Apache commons-io FileSystemUtils is now deprecated. `@deprecated As of 2.6 deprecated without replacement.` – ScrappyDev Nov 18 '22 at 13:24
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.

- 31
- 3
-
1Welcome 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