Add to Google Reader or Homepage

Exception in thread "main" java.io.eofexception

EOFException:

In this post i am going to discuss about the End of File(EOF) exception.EOFexception extends IOException and is thrown while performing I/O operations.

This exception is usually thrown when the end of file (EOF) is reached unexpectedly while reading the input file.Note that,this exception will also be thrown if the input file contains no data in it.

I have explained this EOF exception with the following program.The following program tries to read the file "exam.txt" and prints its contents.When the reader has reached the end of the file i have thrown the EOF exception to indicate that the file has reached its end.

Program:



import java.io.*;

public class Example1
{

public static void main(String[] args)throws IOException
{
BufferedReader br=null;
try
{
File f=new File("E:/exam.txt");
FileReader reader = new FileReader(f);
br = new BufferedReader(reader);
String strcontent =null;
while(true)
{
if((strcontent=br.readLine()) != null)
{
System.out.println(strcontent);
}
else
{
throw new EOFException();
}
}

}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(EOFException e1)
{
br.close();
System.out.println("The file has reached its end");
}
}
}

Thus in the above code the statement "throw new EOFException()" is used to explicily indicate the end of the file and perform the necessary actions rather than letting jvm to deal with it.

In the above program if you do not include the statement throw new EOFException then it will not get terminated.

Sometimes this exception may occur due to an error in the input file.For eg. if the header of the file indicates that the content length of the file is 1000 characters but if you encounter the EOF character after reading some 200 characters.In this situation EOFException can be used to deal with it.

NOTE:


1.When we use Scanner object to read from a file then java.util.NoSuchElementException is thrown if the end of file (EOF) is reached.
2.If we use BufferedReader object to read from a file then no exception will be thrown upon reaching the end of the file.
3. If we use DataInputStream object to read from a file then this EOFException will be thrown if  the reader has reached the end of the file.

I have also explained, how using DataInputStream Object  to read a file throws the EOFException  in the following program.

Program:

import java.io.*;
import java.util.*;

public class Fileclassread
{
public static void main(String args[])
{
String line;

try
{
DataInputStream in=new DataInputStream(new FileInputStream("Child1.class"));
int bytesavailable=in.available();
System.out.println("Total no of bytes in the file "+bytesavailable);

for(int i=0;i
{
if(in.readInt()==0xCAFEBABE)
{
System.out.println("The minor version number:"+in.readShort());
System.out.println("The major version number:"+in.readShort());
}
}
}
catch(EOFException e)
{
System.out.println("the file has reached its end...");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}

output:

Total number of bytes in the file 227
The minor version number:0
The major version number:50
java.io.EOFException
        at java.io.DataInputStream.readInt(Unknown Source)
        at Fileclassread.main(Fileclassread.java:18)

If you see the output of the program,the first line shows how many bytes available in the class file.
Since readInt() will read 4 bytes of data at a time,the for loop should be limited to the value of 55,
because the two readShort() contributes the other 4 bytes.So a total of 224 bytes will be read.

The remaining 3 bytes cannot be read by using readInt() as it requires 4 bytes.Thus java.io.EOFException is thrown.


2 comments:

Saro said...

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.
rpa training in bangalore
rpa training in chennai
rpa training in pune
best rpa training in bangalore

saranya said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
Python training in bangalore
Python course in pune
Python training in bangalore

Post a Comment

 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top