In this blog post, I will be explaining how you can find the total space available in a drive via Java. Consider the following code snippet:
package learnjava.io;
import java.io.File;
public class FindTotalSpaceDemo {
public static void main(String[] args) {
String fileName = "C:/";
File file = new File(fileName);
long totalSpace = file.getTotalSpace();
System.out.println("totalSpace = "+totalSpace 1000000000+ " GB");
}
}
There is a method called File.getTotalspace. This returns the number of bytes available on the partition that is encapsulated by the file object.