开发者

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

开发者 https://www.devze.com 2022-12-24 16:22 出处:网络
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:

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

精彩评论

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

关注公众号