ITMISS
Java 1.7的API稍有不同,可用getTotalSpace(),getUnallocatedSpace()和getUsableSpace()方法通过FileStore类查询可用空间。NumberFormat nf = NumberFormat.getNumberInstance();for (Path root : FileSystems.getDefault().getRootDirectories()) { System.out.print(root + ": "); try { FileStore store = Files.getFileStore(root); System.out.println("available=" + nf.format(store.getUsableSpace()) + ", total=" + nf.format(store.getTotalSpace())); } catch (IOException e) { System.out.println("error querying space: " + e.toString()); }}该API的优势在于,当查询磁盘空间失败时,您可以获取有意义的异常。