- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read DataInputStream until the end without need to catch an 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.
In 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.
Example
Following program demonstrates how to handle the EOFException in Java.
import java.io.DataInputStream; import java.io.DataOutputStream; 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 Scanner sc = new Scanner(System.in); System.out.println("Enter a String: "); String data = sc.nextLine(); byte[] buf = data.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(); //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); } } }
Output
Enter a String: hello how are you helException in thread "main" lo how are youjava.io.EOFException at java.io.DataInputStream.readChar(Unknown Source) at MyPackage.AIOBSample.main(AIOBSample.java:27)
Reading DataInputStream without catching exception
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 Scanner sc = new Scanner(System.in); System.out.println("Enter a String: "); String data = sc.nextLine(); byte[] buf = data.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(); //Reading from the above created file using readChar() method File file = new File("D:\data.txt"); FileInputStream fis = new FileInputStream(file); byte b[] = new byte[(int) file.length()]; fis.read(b); System.out.println("contents of the file: "+new String(b)); } }
Output
Enter a String: Hello how are you contents of the file: H e l l o h o w a r e y o u
- Related Articles
- How to handle EOFException in java?
- How to catch an assertion error in Java
- Need Selenium to wait until the document is ready
- End-to-End Encryption - How It Works, and Why We Need It?
- How to make an ArrayList read only in Java?
- Can finally block be used without catch in Java?
- Need to wait until page is completely loaded - Selenium WebDriver
- How to read data from scanner to an array in java?
- How to read the contents of a web page without using any external library in Java?
- How to use @Until annotation using the Gson library in Java?
- What is EOFException in Java? How do we handle it?
- How to define an array size in java without hardcoding?
- How to wait until an element is present in Selenium?
- How to convert/read an input stream into a string in java?
- How to wait until an element no longer exists in Selenium?
