Java 7 NIO offers FileStore class to check available size
Path p = Paths.get("/your/file"); // where you want to write FileSystem fileSystem = FileSystems.getDefault(); Iterable<FileStore> iterable = fileSystem.getFileStores(); Iterator<FileStore> it = iterable.iterator(); // iterate the FileStore instances while(it.hasNext()) { FileStore fileStore = it.next(); long sizeAvail = fileStore.getUsableSpace(); // or maybe getUnallocatedSpace() if (Files.getFileStore(p).equals(fileStore) { // your Path belongs to this FileStore if (sizeAvail > theSizeOfBytesYouWantToWrite) { // do your thing } } }
Obviously, you can still get an IOException , since nothing is atomic, and other processes can use the same drive, so keep that in mind and handle the exception accordingly.
source share