How To Find Total Space, Free Space And Usable Space In A Drive In Java?
There are three methods introduced in JDK 1.6 related to disk usage. They are getTotalSpace(), getFreeSpace() and getUsableSpace(). All these three methods are members of java.io.File class. Let’s see brief description about these methods.
1) long java.io.File.getTotalSpace() :
This method returns total size of a drive in bytes.
2) long java.io.File.getFreeSpace() :
This method returns free space available in a drive in bytes.
3) long java.io.File.getUsableSpace() :
This method returns number of bytes available to this virtual machine in a drive. This method checks write permissions and other operating system restrictions on a drive. Hence gives more accurate information on free space available in a drive than getFreeSpace() method.
getFreeSpace() Vs getUsableSpace() : getFreeSpace() returns total free space available in a drive to all the users. But, getUsableSpace() returns free space available in a drive to the current user. In some operating systems, some space is reserved to admin user. In such systems, getUsableSpace() gives more accurate information on free space available in a drive.