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 anIOException
. 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:
IOExceptionExample.java:
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) { System.out.println(curline); } } catch (IOException e) { System.err.println("An IOException was caught :"+e.getMessage()); }finally{ try { if(br != null) br.close(); } catch (IOException e) { e.printStackTrace(); } } } }
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)
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