java.io.File; File file = new File("c:"); //取得C:\ 的總total空間 file.getTotalSpace(); //取得剩餘空間 file.getFreeSpace(); //取得已使用的空間 file.getUsableSpace();
範例 2 號:
/* Get a list of all filesystem roots on this system */
File[] roots = File.listRoots();
/* For each filesystem root, print some info */
for (File root : roots) {
System.out.println("File system root: " + root.getAbsolutePath());
System.out.println("Total space (bytes): " + root.getTotalSpace());
System.out.println("Free space (bytes): " + root.getFreeSpace());
System.out.println("Usable space (bytes): " + root.getUsableSpace());
}