- java.io.IOException – How to solve Java IOException
- 1. Prerequisites
- 2. Download
- 3. Setup
- 4. What is Java IOException – java.io.IOException
- 5. UML diagram
- 6. When is IOException thrown
- 6. A simple case of java.io.ioexception
- 7. How to solve java.io.IOException
- 8. Download the Source Code
- Java.IO.Ioexception
- the java.io.IOException in Java
- Related Article — Java Error
java.io.IOException – How to solve Java IOException
In this article, we will explain how to solve the java.io.IOException.
This exception is related to Input and Output operations in the Java code. It happens when there is a failure during reading, writing, and searching file or directory operations. IOException is a checked exception. A checked exception is handled in the java code by the developer. This exception object has a string message which is the root cause for the failure.
IOException has subclasses such as FileNotFoundException , EOFException , UnsupportedEncodingException , SocketException , and SSLException . If the file is not found, FileNotFoundException is thrown. While reading a file, EOFException occurs when the end of the file is reached. If the file has an unsupported encoding, UnsupportedEncodingException occurs. When the socket connection is closed, SocketException can happen. SSLException happens when the SSL connection is not established.
1. Prerequisites
Java 7 or 8 is required on the Linux, windows, or Mac operating system.
2. Download
You can download Java 7 from the Oracle site. On the other hand, You can use Java 8. Java 8 can be downloaded from the Oracle website.
3. Setup
You can set the environment variables for JAVA_HOME and PATH. They can be set as shown below:
JAVA_HOME="/desktop/jdk1.8.0_73" export JAVA_HOME PATH=$JAVA_HOME/bin:$PATH export PATH
4. What is Java IOException – java.io.IOException
java.io.IOException is an exception which programmers use in the code to throw a failure in Input & Output operations. It is a checked exception. The programmer needs to subclass the IOException and should throw the IOException subclass based on the context.
5. UML diagram
The sequence diagram of throwing the IOException in classes Manager, Service, Facade, and Persistence Manager is shown below:
6. When is IOException thrown
Java application needs to handle failures related to reading, writing, and searching a file or a directory. java.io.IOException is the base exception class used for handling the failures. In a method of a class, try, catch, and finally block handles the exception. The application API class methods throw an IOException or its subclasses.
Try catch finally block of code is shown below in different scenarios. The code below shows the printing of the exception stack trace. Printing Stack trace
try < >catch(IOException ioException) < ioException.printStacktrace(); >finally
In the code below, a runtime exception is thrown after catching the IOException in a java application. throwing a runtime exception
try < >catch(IOException ioException) < throw new RuntimeException("IO Exception in CommandLine Application",ioException); >finally
In the code below, a wrapped exception is thrown after catching IOException in Facade class. throwing a wrapped exception
try < >catch(IOException ioException) < throw new WrappedException("IO Exception in Facade" ,ioException); >finally
In the code below, throwing a business exception after catching the IOException is shown. Throwing a business exception
try < >catch(IOException ioException) < throw new BusinessException("IO Exception in Service" ,ioException); >finally
Throwing an application exception after catching an IOException is presented in the code below: Throwing an application exception
try < >catch(IOException ioException) < throw new ApplicationException("IO Exception in Manager" ,ioException); >finally
6. A simple case of java.io.ioexception
Let’s see a very simple case of a Java IOException . In the following example, we are going to try to read some lines of text from a file that does not exist: IOException
import java.io.FileInputStream; import java.io.FileNotFoundException; public class FileNotFoundExceptionExample < public void checkFileNotFound() < try < FileInputStream in = new FileInputStream("input.txt"); System.out.println("This is not printed"); >catch (FileNotFoundException fileNotFoundException) < fileNotFoundException.printStackTrace(); >> public static void main(String[] args) < FileNotFoundExceptionExample example = new FileNotFoundExceptionExample(); example.checkFileNotFound(); >>
The code above is executed as shown below: Run Command
javac InputOutputExceptionExample.java java InputOutputExceptionExample
Now, when you run this program because the file input.txt does not exist, the exception is thrown as shown in the screen below. As you can see the message is showing the cause of the problem. The root cause of the problem is that the file does not exist.
EOFException is a subclass of the IOException.The code below shows how an EndOfFileException happens while reading an input file. While reading a file, EOFException is thrown when the end of the file is reached. EndOfFileException
import java.io.DataInputStream; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class EndOfFileExceptionExample < public void checkEOF() < File file = new File("einput.txt"); DataInputStream dataInputStream = null; try < dataInputStream = new DataInputStream(new FileInputStream(file)); while(true) < dataInputStream.readInt(); >> catch (EOFException eofException) < eofException.printStackTrace(); >catch (IOException ioException) < ioException.printStackTrace(); >finally < try< if (dataInputStream != null) < dataInputStream.close(); >> catch (IOException ioException) < ioException.printStackTrace(); >> > public static void main(String[] args) < EndOfFileExceptionExample example = new EndOfFileExceptionExample(); example.checkEOF(); >>
The code above is executed as shown below: Run Command
javac EndOfFileExceptionExample.java java EndOfFileExceptionExample
You can run the above code as per the command above. The output is as shown on the screen below.
FileNotFoundException is a subclass of IOException. FileNotFoundException scenario is presented in the code below. This happens if the input file is not found. FileNotFoundException
import java.io.FileInputStream; import java.io.FileNotFoundException; public class FileNotFoundExceptionExample < public void checkFileNotFound() < try < FileInputStream in = new FileInputStream("input.txt"); System.out.println("This is not printed"); >catch (FileNotFoundException fileNotFoundException) < fileNotFoundException.printStackTrace(); >> public static void main(String[] args) < FileNotFoundExceptionExample example = new FileNotFoundExceptionExample(); example.checkFileNotFound(); >>
The code above is executed as shown below: Run Command
javac FileNotFoundExceptionExample.java java FileNotFoundExceptionExample
The output of the code when executed is shown below.
7. How to solve java.io.IOException
IOException is a Java exception that occurs when an IO operation fails. Develop can explicitly handle the exception in a try-catch-finally block and print out the root cause of the failure. The developer can take the correct actions to solve this situation by having additional code in the catch and finally blocks.
8. Download the Source Code
That was an example of how to solve the java.io.ioexception.
Download
You can download the full source code of this example here: java.io.ioexception – How to solve Java IOException
Last updated on Oct. 12th, 2021
Java.IO.Ioexception
This tutorial demonstrates the java.io.ioexception in Java.
the java.io.IOException in Java
- Reading a file, but that file doesn’t exist.
- Reading a file but don’t have permission.
- Writing a file, but the disc space is not available.
- When reading a stream, but that stream is closed by some other process.
- When reading a network file, but that network is disconnected.
The Java.IO.IOexception provides exceptions for system input and output data streams and serialization.
The above exceptions are the types of subclasses of the main IO exception. Let’s try an example that throws the IOException in Java:
package delftstack; import java.io.FileInputStream; import java.io.IOException; public class Example public static void main(String[] args) throws Exception FileInputStream File_Input_Stream = new FileInputStream("Demofile.txt"); System.out.println("Check if the file is there. "); > >
The code above will throw the IO FileNotFoundException because the file we are accessing doesn’t exist. See output:
Exception in thread "main" java.io.FileNotFoundException: Demofile.txt (The system cannot find the file specified) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:216) at java.base/java.io.FileInputStream.(FileInputStream.java:157) at java.base/java.io.FileInputStream.(FileInputStream.java:111) at delftstack.Example.main(Example.java:8)
To handle these exceptions, we use try-catch blocks to stop JVM from crashing the code. See the solution:
package delftstack; import java.io.FileInputStream; import java.io.IOException; public class Example public static void main(String[] args) throws Exception try FileInputStream File_Input_Stream = new FileInputStream("Demofile.txt"); System.out.println("Check if the file is there. "); > catch (IOException e) //e.printStackTrace(); or System.out.println(e.getMessage()); > > >
Now the code above will print the same error information but without crashing the code. See output:
Demofile.txt (The system cannot find the file specified)
Let’s try another example that throws the End of File exception ( EOFException ) in Java:
package delftstack; import java.io.DataInputStream; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class Example public void DemoCheck() File file = new File("delftstack.txt"); DataInputStream Data_Input_Stream = null; try Data_Input_Stream = new DataInputStream(new FileInputStream(file)); while(true) Data_Input_Stream.readInt(); > > catch (EOFException E_O_F_Exception) E_O_F_Exception.printStackTrace(); > catch (IOException I_O_Exception) I_O_Exception.printStackTrace(); > finally try if (Data_Input_Stream != null) Data_Input_Stream.close(); > > catch (IOException I_O_Exception) I_O_Exception.printStackTrace(); > > > public static void main(String[] args) Example DemoExample = new Example(); DemoExample.DemoCheck(); > >
The code throws EOFException because it’s trying to read a file, and the end of the file has arrived. See output:
java.io.EOFException at java.base/java.io.DataInputStream.readInt(DataInputStream.java:398) at delftstack.Example.DemoCheck(Example.java:16) at delftstack.Example.main(Example.java:39)
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.