
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to handle EOFException in java?
While reading the contents of a file in certain scenarios the end of the file will be reached in such scenarios a EOFException is thrown.
Especially, this exception is thrown while reading data using the Input stream objects. In other scenarios a specific value will be thrown when the end of file reached.
Example
Let us consider the DataInputStream class, it provides various methods such as readboolean(), readByte(), readChar() etc.. to read primitive values. While reading data from a file using these methods when the end of file is reached an EOFException is thrown.
import java.io.DataInputStream; import java.io.FileInputStream; public class EOFExample { public static void main(String[] args) throws Exception { //Reading from the above created file using readChar() method DataInputStream dis = new DataInputStream(new FileInputStream("D:\data.txt")); while(true) { char ch; ch = dis.readChar(); System.out.print(ch); } } }
Runtime Exception
Hello how are youException in thread "main" java.io.EOFException at java.io.DataInputStream.readChar(Unknown Source) at SEPTEMBER.remaining.EOFExample.main(EOFExample.java:11)
Handling EOFException
You cannot read contents of a file without reaching end of the file using the DataInputStream class. If you want, you can use other sub classes of the InputStream interface.
Example
In the following example we have rewritten the above program using FileInputStream class instead of DataInputStream to read data from a file.
import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Scanner; public class AIOBSample { public static void main(String[] args) throws Exception { //Reading data from user byte[] buf = " Hello how are you".getBytes(); //Writing it to a file using the DataOutputStream DataOutputStream dos = new DataOutputStream(new FileOutputStream("D:\data.txt")); for (byte b:buf) { dos.writeChar(b); } dos.flush(); System.out.println("Data written successfully"); } }
Output
Data written successfully
Following is another way to handle EOFException in Java −
import java.io.DataInputStream; import java.io.EOFException; import java.io.FileInputStream; import java.io.IOException; public class HandlingEOF { public static void main(String[] args) throws Exception { DataInputStream dis = new DataInputStream(new FileInputStream("D:\data.txt")); while(true) { char ch; try { ch = dis.readChar(); System.out.print(ch); } catch (EOFException e) { System.out.println(""); System.out.println("End of file reached"); break; } catch (IOException e) { } } } }
Output
Hello how are you End of file reached
- Related Articles
- What is EOFException in Java? How do we handle it?
- How to handle StringIndexOutOfBoundsException in Java?
- How to handle MalformedURLException in java?
- How to read DataInputStream until the end without need to catch an EOFException in java?
- How to handle Assertion Error in Java?
- How to handle proxy in Selenium in Java?
- How to handle the NumberFormatException (unchecked) in Java?
- How to handle the StringIndexOutOfBoundsException (unchecked) in Java?
- How to handle the Runtime Exception in Java?
- How to handle action event for JComboBox in Java?
- How to handle the ArithmeticException (unchecked) in Java?\n
- How to handle the ArrayStoreException (unchecked) in Java?\n
- How to handle the exception using UncaughtExceptionHandler in Java?
- How to handle frame in Selenium WebDriver using java?
- How to handle an exception in JShell in Java 9?
