IOException in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The Java IOException is a checked exception that must be handled at compilation time. IOException is the base class for exceptions thrown while accessing data from files, directories, and streams. It represents a class of exceptions that are thrown when an I/O error occurs.

The most common cause due to which an IOException is thrown is attempting to access a file that does not exist at the specified location. Common exception classes that are derived from the IOException base class are EndOfStreamException, FileNotFoundException, DirectoryNotFoundException, etc.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

What is IOException in Java?

The IOException is simply an exception thrown when an I/O error occurs. It is also the base class of such exceptions that occur while reading or accessing files, directories, and streams.

The IOException belongs to the class of checked exceptions. Checked exceptions are thrown at compile time. Resolving checked exceptions is necessary to execute a Java program.

There are many subclasses of IOException, such as FileNotFoundException, SSLException, EndOfStreamException, and DirectoryNotFoundException.

When IOException Occurs in Java?

Let's see an example where IOException is thrown at compile-time:

Output:

Explanation:

  • FileNotFoundException is thrown when compiling the above Java program. Hence, the program does not compile and cannot be executed.
  • This exception is thrown to avoid scenarios such as if the named file does not exist, is a directory rather than a regular file, or cannot be opened for reading for some other reason.
  • Apart from the close() and read() methods of the FileReader class, it also throws an IOException, which must be handled.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

How to Handle IOException in Java?

We can handle IOException using the try and catch blocks to handle the exception.

Let's understand how to use the try and catch block to handle the IOException in java.

Output:

Explanation:

  • In the above case, We have used try and catch blocks to handle IOException.
  • The above program compiles successfully and does not throw any exception at compile time.
  • However, if the specified file does not exist or any other error occurs, the code within the catch block executes as shown in the output.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

IOException Examples

Let's understand the examples of IOExceptions.

UnsupportedEncodingException: This exception occurs when we try to encode the String using an invalid encoding scheme. Let's understand this using an example.

Output:

Explanation:

  • In the above program, we encode a given string using a not-supported format.
  • In such cases, UnsupportedEncodingException is thrown, as shown in the output.

Conclusion

  • IOException is a checked exception which occurs at compile time. It must be resolved before executing a Java program.
  • IOException is the base class of many checked exceptions thrown while reading files, directories, and streams.
  • The try and catch block avoids IOException.
  • FileNotFoundException, UnsupportedEncodingException, and DirectoryNotFoundException are some of the subclasses of the IOException class.