Find free disk space using Java
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);
}
}
chevron_rightfilter_noneOutput:
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);
}
}
chevron_rightfilter_noneOutput:
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);
}
}
chevron_rightfilter_noneOutput:
62.857 GB
Reference used: Oracle
This article is contributed by Mayank Kumar. If you like GeeksforGeeks 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 GeeksforGeeks 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.
Recommended Posts:
- How to find max memory, free memory and total memory in Java?
- Find max or min value in an array of primitives using Java
- Find first and last element of ArrayList in java
- Matcher find(int) method in Java with Examples
- Find the index of an array element in Java
- How to find the length or size of an ArrayList in Java
- Java program to find IP address of your computer
- Find common elements in two ArrayLists in Java
- Matcher find() method in Java with Examples
- Quick ways to check for Prime and find next Prime in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.util.concurrent.Phaser class in Java with Examples