Java FileStore class allows you to access information about a systems files store such as available space, total space and used space.
FileStore
private static final String SOURCE = "com/levelup/java/io/get-used-space.txt"; Path source; @Before public void setUp () throws IOException, URISyntaxException { source = Paths.get(this.getClass().getClassLoader().getResource(SOURCE).toURI()); }
@Test public void get_used_space () { File file = source.toFile(); long usedSpace = (file.getTotalSpace() - file.getFreeSpace()) / 1024; assertTrue(usedSpace > 0); }
@Test public void get_used_space_nio () throws IOException { FileStore store = Files.getFileStore(source); long usedSpace = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024; assertTrue(usedSpace > 0); }