Home » Core Java » io » IOException » java.io.IOException – How to solve IOException

About Nikos Maravitsas

Nikos Maravitsas
Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

java.io.IOException – How to solve IOException

In this example we are going to talk about a very common exception that many java developers stumble upon when dealing with IO operations in their program : IOException. This exception occurs when an IO operation has failed for some reason. It is also a checked exception which means that your program has to handle it. All bult-in Java IO methods that might cause an IOException, explicitly throw it so that your program can handle it. It is also worth noting that the IOException object that your program receives is accompanied by a String message that can inform you as accurately as possible for the cause of the exception.

Also, IOException is the most general class that can describe an IOException. Several subclass of it exist in order to reveal the problem that occurred in a more specific and detailed manner. Some of the most well know sub classes are FileNotFoundException, EOFException, UnsupportedEncodingException, SocketException, SSLException

1. A simple case of IOException

Let’s see a very simple case of an IOException. In the following example we are going to try to read some lines of text form a file that does not exist:


package com.javacodegeeks.core.io.ioexception;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class IOExceptionExample {

	private static String filepath = "C:\\Users\\nikos\\Desktop\\TestFiles\\testFile2.txt";
	public static void main(String[] args) {
		BufferedReader br = null;
		String curline;
		try {
			br = new BufferedReader(new FileReader(filepath));

			while ((curline = br.readLine()) != null) {

		} catch (IOException e) {
			System.err.println("An IOException was caught :"+e.getMessage());
			try {
				if(br != null)
			} catch (IOException e) {



Now, when I run this program, because the file C:\\Users\\nikos\\Desktop\\TestFiles\\testFile2.txt does not exist, it will output:

An IOException was caught :C:\Users\nikos\Desktop\TestFiles\testFile2.txt (The system cannot find the file specified)

As you can see the message is pretty informative of the cause of the problem. Note that when the file you are trying to access does not exist, a more “specialized” exception is thrown, FileNotFoundException.

2. How to solve IOException

As you’ve seen IOException is a very general exception that occurs when an IO operations fails. So you can understand that there is no standard way on how to solve this exception. The best thing you can do is to explicitly handle the exception in a try-catch block and print out the message of the exception. Then you can take the correct actions to solve this situation. For example, in the previous code snippet, the message clearly states that the file does not exist. So you should go ahead and create the file. It can also say :”Permission Denied”. This means that you should check if you have the permission to perform the action you requested in that file. If you are working with streams and especially with sockets and the stream is closed in the middle of your session you could see a message like :”Stream Closed” that designates the problem exactly .

Download Source Code

This was an example on java.io.IOException. Download the source code of this example here : IOExceptionExample.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply

Be the First to Comment!

Notify of