java 获取磁盘空间的大小的方法

import java.io.File;
/**
 * 计算磁盘空间大小
 * @author admin
 *
 */
public class SpaceChecker {
     public static void main(String[] args) {
         File[] roots = File.listRoots();
         for (File _file : roots) {
             System.out.println(_file.getPath());
             //System.out.println(_file.getName());
             System.out.println("Free space = " + _file.getFreeSpace());
             System.out.println("Usable space = " + _file.getUsableSpace());
             System.out.println("Total space = " + _file.getTotalSpace());
             System.out.println("used space  = " + (_file.getTotalSpace()-_file.getFreeSpace()));
             System.out.println();
         }
         File win = new File("C:\\WINDOWS");
         System.out.println(win.getPath());
         System.out.println(win.getName());
         System.out.println("Free space = " + win.getFreeSpace());
         System.out.println("Usable space = " + win.getUsableSpace());
         System.out.println("Total space = " + win.getTotalSpace());
         System.out.println();
     }
}

你可能感兴趣的:(java 磁盘空间大小)