开发者

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

I used the SystemEnvironment class in Java for gettin开发者_运维问答g 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?


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());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜