Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between getFreespace() and getUsableSpace of File

Tags:

java

I could not point out exact difference between getFreeSpace() and getUsableSpace() method of File class. When i run following code , got same o/p.

Class Start {
    public static void main(String [] args) {
        File myfile = new File("C:\\html\abc.txt");
        myfile.createNewFile();
        System.out.println("free space "+myfile.getFreeSpace()+"usable space "+myfile.getUsableSpace());
    }
}

O/P is

free space 445074731008 usable space 445074731008

Could any one tell me what is exact difference ?

like image 308
sar Avatar asked Jan 29 '14 18:01

sar


People also ask

What is the use of Java IO file API?

Uses of File in java.io. Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. Returns the absolute form of this abstract pathname.

What is a file write a short note on file class?

The File class is an abstract representation of file and directory pathname. A pathname can be either absolute or relative.


2 Answers

The java.io.File.getFreeSpace() method returns the number of unallocated bytes in the partition named by this abstract path name. The returned number of unallocated bytes are not a gurantee. The count of unallocated bytes is likely to be accurate immediately after this call and inaccurate by any external I/O operations.

The java.io.File.getUsableSpace() method returns the number of bytes available to this virtual machine on the partitioned named by this abstract name. This method usually provide a more accurate estimate of how much new data can actually be written as this method checks for write permissions and other operating system restrictions.

like image 160
Kick Avatar answered Sep 28 '22 01:09

Kick


The difference is quite clearly stated in the Javadoc of the two methods:

getFreeSpace():

Returns the number of unallocated bytes in the partition named by this abstract path name. [...]

getUsableSpace():

Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname. When possible, this method checks for write permissions and other operating system restrictions and will therefore usually provide a more accurate estimate of how much new data can actually be written than getFreeSpace(). [...]

However, on most systems the two methods return the exact same number.

like image 44
Itchy Avatar answered Sep 27 '22 23:09

Itchy