logo

How to get free, available and total disk space in Java?

Since Java SE 7 you can use FileStore to retrieve these information.

Since Java SE 7 you can use FileStore to retrieve these information:

FileStore fileStore = Files.getFileStore(Paths.get("c:/"));
long totalSpaceInBytes = fileStore.getTotalSpace();
long freeSpaceInBytes = fileStore.getUsableSpace();
long usedSpaceInBytes = totalSpaceInBytes - freeSpaceInBytes;
System.out.println("total space in GB: " + (totalSpaceInBytes / 1024 / 1024 / 1024));
System.out.println("free space in GB: " + (freeSpaceInBytes / 1024 / 1024 / 1024));
System.out.println("used space in GB: " + (usedSpaceInBytes / 1024 / 1024 / 1024));

If you use Java SE 6, you will find these methods in File class. Or you can use Apache Commons IO library, where's FileSystemUtils class, which does the same.


comments powered by Disqus

rss

Free online Java web application examples

Want to develop whole web applications which use database, security and more? See my github account, where are lot's of example projects:
github

Search