Java 1.7 has a slightly different API, free space can be requested through the FileStore class via getTotalSpace () , getUnallocatedSpace () and getUsableSpace () methods .
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()); } }
The advantage of this API is that you get significant exceptions when a disk space request fails.
prunge Oct. 19 '11 at 3:24 a.m. 2011-10-19 03:24
source share