Venkat
Venkat

Reputation: 21490

Can i get the particular disk space(like C: ) using Java program?

I used the SystemEnvironment class in Java for getting system information.

In that I can get only RAM size, I can't get the specific disk space like c: and D:

code is,

com.sun.management.OperatingSystemMXBean mxbean = (com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean(); System.out.println("Total RAM:"+mxbean.getTotalSwapSpaceSize()/(1024*1024*1024)+""+"GB");

Can i get this in information in a Java program?

Upvotes: 1

Views: 402

Answers (1)

polygenelubricants
polygenelubricants

Reputation: 383966

From java.io.File API:

  • long getTotalSpace(): Returns the size of the partition named by this abstract pathname.
  • long getUsableSpace(): Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname.
  • long getFreeSpace(): Returns the number of unallocated bytes in the partition named by this abstract path name.

The following snippet shows an example on how to use it:

for (File root : File.listRoots()) {
    System.out.format("%s (%d bytes free)%n", root, root.getFreeSpace());
}

Upvotes: 9

Related Questions