-3

I want to build an application that gives me a warning when my harddrive is about to become full. For this the program needs to retrieve the amount of available disk space. Anyone kows how I can do this?

gefdel
  • 59
  • 1
  • 7

1 Answers1

1

Yes, you can. You should use File class methods to do that. Tsadak's method works for Windows only. More universal method is this:

NumberFormat numFormat= NumberFormat.getNumberInstance(); for (Path fsRoot : FileSystems.getDefault().getRootDirectories()) {

System.out.print(root + ": ");
try {
    FileStore store = Files.getFileStore(root);
    System.out.println("available space" + numFormat.format(store.getUsableSpace()));
System.out.println(", total=" + numFormat.format(store.getTotalSpace()));
} catch (IOException e) {
    System.out.println("error: " + e.toString());
}
}
sixtytrees
  • 1,156
  • 1
  • 10
  • 25