How do I get total space and free space of my disk?
Category: java.io, viewed: 1487 time(s).
package org.kodejava.sample.java.io; import java.io.File; public class FreeSpaceExample { public static void main(String[] args) { // We create an instance of a File to represent a partition // of our file system. For instance here we used a drive D: // as in Windows operating system. File file = new File("D:"); // Using the getTotalSpace() we can get an information of // the actual size of the partition, and we convert it to // mega bytes. long totalSpace = file.getTotalSpace() / (1024 * 1024); // Next we get the free disk space as the name of the // method shown us, and also get the size in mega bytes. long freeSpace = file.getFreeSpace() / (1024 * 1024); // Just print out the values. System.out.println("Total Space = " + totalSpace + " Mega Bytes"); System.out.println("Free Space = " + freeSpace+ " Mega Bytes"); } } |
Here is the result of the program:
Total Space = 76316 Mega Bytes Free Space = 58412 Mega Bytes
Can't find what you are looking for? Join our FORUMS and ask some questions!
Related Examples
- How do I store properties as XML file?
- How do I load properties from XML file?
- How do I convert InputStream to String?
- How do I convert string into InputStream?
- How do I get an exception stack trace message?
- How do I use Console class to read user input?
- How do I store objects in file?
- How do I use DataInputStream and DataOutputStream?
- How do I use RandomAccessFile class?
- How do I create a directories recursively?