How to Fix java.io.IOException: No Space Left on Device in Java

Mehvish Ashiq Feb 02, 2024
How to Fix java.io.IOException: No Space Left on Device in Java

Today, we will figure out the cause of the java.io.IOException: No space left on device error while programming in Java. Further, we will also learn about the possible solutions to eliminate this error.

Causes and Possible Solutions for the java.io.IOException: No space left on device Error

Let’s have a scenario to understand the error and find its reason. Suppose we have a program that writes a lot of data to a file, but after some time, we face an error saying No space left on device.

It means we don’t have enough disk space on our device to write all the required data on a file saved on this specific disk. Usually, it happens while working with large instances with a high number of pages that are being created.

How to resolve this error? Some of the solutions are given below:

Solution 1

Java 7 Java New Input/Output (NIO) provides the FileStore class that we can use to ensure that we have enough space to write. Following is an example code demonstrating how to use it.

// write the path where you want to write
Path path = Paths.get("/yourPath/fileName");
FileSystem fileSystem = FileSystems.getDefault();
Iterable<FileStore> iterable = fileSystem.getFileStores();

// iterate over the instances of FileStore
Iterator<FileStore> iterator = iterable.iterator();
while (iterator.hasNext()) {
  FileStore fileStore = iterator.next();
  // you may be able to use or getUnallocatedSpace()
  // instead of getUsableSpace() method
  long sizeAvail = fileStore.getUsableSpace();
  // your given Path belongs to this specific FileStore
    if (Files.getFileStore(path).equals(fileStore) {
    if (sizeAvail > theSizeOfBytesYouWantToWrite) {
      // do your stuff
    } // end if
    }//end if
} // end while

You can probably still face an IOException because nothing is atomic. Other processes might be using and storing data on the same disk.

So, it is important to keep that in mind and handle the exception according to the situation.

Solution 2

The try-catch block can also do the trick if we want to handle it in a way that stops writing if there is no more space.

Remember that it is not practical to avoid getting this error in the first place. We can check the availability of the required space first if we already know how much disk space we need.

Again, we might still face this error if any other application is writing to the same disk, so this type of error handling depends on what situation is causing this error. Most applications deal with it in some way when they face this error.

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

Related Article - Java Error