通过java得到windows的磁盘空间大小

时间:2014-12-18 10:28:49   收藏:0   阅读:199

可以通过java的File对象得到磁盘的总空间大小,剩余空间大小,以及已用空间大小。


  1. import java.io.File;  

  2.   

  3. public class FreeDiskSpace {  

  4.   

  5.     public static void main(String[] args) {  

  6.         File file = new File("c:");  

  7.         long totalSpace = file.getTotalSpace();  

  8.         long freeSpace = file.getFreeSpace();  

  9.         long usedSpace = totalSpace - freeSpace;  

  10.   

  11.         System.out.println("总大小 : " + totalSpace / 1024 / 1024 / 1024 + "G");  

  12.         System.out.println("剩余大小 : " + freeSpace / 1024 / 1024 / 1024 + "G");  

  13.         System.out.println("已用大小 : " + usedSpace / 1024 / 1024 / 1024 + "G");  

  14.     }  

  15.   

  16. }  


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!