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

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

+58
java diskspace
Jun 26 '09 at 21:09
source share
6 answers

Check out 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 are still using 1.5, then you can use the Apache Commons IO library and its FileSystem class .

+86
Jun 26 '09 at 21:13
source share
— -

Java 1.7 has a slightly different API, free space can be requested through the FileStore class via 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 significant exceptions when a disk space request fails.

+55
Oct. 19 '11 at 3:24 a.m.
source share
+20
Jun 26 '09 at 21:14
source share

Link

If you are a Java programmer, you may have already been asked these simple, stupide question: "How to find free disk space left on my system?". The problem is that the answer is system dependent. This is actually an implementation that is system dependent. And until recently, there was no unequivocal decision to answer this question, although it has been recorded in the Suns Bug database since June 1997. Now you can get free space in Java 6 using the method in the class file, which returns the number of unallocated bytes in the section called the abstract path name. But you may be interested in using disk space (the one that is writable). It is even possible to get the total disk space of a partition with the getTotalSpace () method.

+6
Jun 26 '09 at 21:12
source share

when checking disk space using java you have the following method in java.io file class

  • getTotalSpace ()
  • getFreeSpace ()

which will definitely help you in obtaining the necessary 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 of using these methods.

+3
Apr 02 '13 at 16:53
source share
 public class MemoryStatus{ public static void main(String args[])throws Exception{ Runtime r=Runtime.getRuntime(); System.out.println("Total Memory: "+r.totalMemory()); System.out.println("Free Memory: "+r.freeMemory()); } } 

Try this code to get disk space.

-5
Jan 29 '18 at 8:03
source share



All Articles