There are a few method calls in java that can be utilized to get the space related information about a disk drive. These methods used to fetch the such information are declared in File class, which resides in java.io package. The details of these method call along with their use are given below:
Note: These codes will not run on online ide. Also, they will all work on Java 1.6 and above versions
- Java.io.File.getFreeSpace(): Get total free space available in a drive
Syntax:public long getFreeSpace() Returns the size of the partition named by this abstract pathname. Returns: The size, in bytes, of the partition or 0L if this abstract pathname does not name a partition Throws: SecurityException - If a security manager has been installed and it denies RuntimePermission("getFileSystemAttributes") or its SecurityManager.checkRead(String) method denies read access to the file named by this abstract pathname
// Java program to get the amount of free space available on any drive
import
java.io.*;
public
class
Test
{
public
static
void
main(String[] args)
{
File file =
new
File(
"E:\\"
);
double
size = file.getFreeSpace() / (
1024.0
*
1024
*
1024
);
System.out.printf(
"%.3f GB\n"
, size);
}
}
Output:
18.242 GB
- Java.io.File.getUsableSpace(): Total usable space available with a drive. 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().
The returned number of available bytes is a hint, but not a guarantee, that it is possible to use most or any of these bytes. The number of unallocated bytes is most likely to be accurate immediately after this call. It is likely to be made inaccurate by any external I/O operations including those made on the system outside of this virtual machine. This method makes no guarantee that write operations to this file system will succeed.
Syntax:public long getUsableSpace() Returns: The number of available bytes on the partition or 0L if the abstract pathname does not name a partition. On systems where this information is not available, this method will be equivalent to a call to getFreeSpace(). Throws: SecurityException - If a security manager has been installed and it denies RuntimePermission("getFileSystemAttributes") or its SecurityManager.checkRead(String) method denies read access to the file named by this abstract pathname
import
java.io.*;
public
class
Test
{
public
static
void
main(String[] args)
{
double
size =
new
File(
"C:\\"
).getUsableSpace() / (
1024.0
*
1024
*
1024
);
System.out.printf(
"%.3f GB\n"
, size);
}
}
Output:
62.857 GB
- Java.io.File.getTotalSpace(): Total Capacity of a drive. The method returns the size of the partition named by this abstract pathname.
Syntax:
public long getTotalSpace() Returns: Returns: The size, in bytes, of the partition or 0L if this abstract pathname does not name a partition Throws: SecurityException - If a security manager has been installed and it denies RuntimePermission("getFileSystemAttributes") or its SecurityManager.checkRead(String) method denies read access to the file named by this abstract pathname
import
java.io.*;
public
class
Test
{
public
static
void
main(String[] args)
{
double
size =
new
File(
"C:\\"
).getUsableSpace() / (
1024.0
*
1024
*
1024
);
System.out.printf(
"%.3f GB\n"
, size);
}
}
Output:
62.857 GB
Reference used: Oracle
This article is contributed by Mayank Kumar. If you like Lazyroar and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.