Java IOjava-io-examples

How to find the total space in a drive in Java

Posted On
Posted By admin

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.

If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment