EOFException in Java

EOFException: 

java.io.EOFException

Signals that an end of file or end of the stream has been reached unexpectedly during input.

This exception is mainly used by data input streams to signal the end of the stream.

Note that many other input operations return a special value at end of the stream rather than throwing an exception.

Example: 

Java

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;

public class EOFExceptionExample {
    public static void main(String[] args) throws Exception {

        DataInputStream dis =
new DataInputStream(new FileInputStream("D:\\hello.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) {
                System.out.println("IOException occurs");
            }
        }
        dis.close();
    }

}

Output:

Hello Java

End of file reached


Constructor of EOFException

Example 1:

java.io.EOFException.EOFException()

Constructs an EOFException with null as its error detail message.

Java

import java.io.EOFException;

public class EOFExceptionExample {
    public static void main(String[] args) throws Exception {

        EOFException eofException = new EOFException();
        System.out.println(eofException);

    }

}

Output:

java.io.EOFException

Example 2:

java.io.EOFException.EOFException(String s)

Constructs an EOFException with the specified detail message. The string s may later be retrieved by the java.lang.Throwable.getMessage method of class java.lang.Throwable.

Parameters:

1. s the detailed message.

Java

import java.io.EOFException;

public class EOFExceptionExample {
    public static void main(String[] args) throws Exception {

        EOFException eofException = new EOFException("hello");
        System.out.println(eofException);

    }

}

Output:

java.io.EOFException: hello


Some other Exceptions of java.io

FileNotFoundExceptionSignals that an attempt to open the file denoted by a specified path name has failed.

InterruptedIOExceptionSignals that an I/O operation has been interrupted. 

InvalidClassExceptionThrown when the Serialization runtime detects.

InvalidObjectExceptionIndicates that one or more deserialized objects failed validation tests.

IOErrorThrown when a serious I/O error has occurred.

IOExceptionSignals that an I/O exception of some sort has occurred.

NotActiveExceptionThrown when serialization or deserialization is not active.

NotSerializableExceptionThrown when an instance is required to have a Serializable interface.

ObjectStreamExceptionSuperclass of all exceptions specific to Object Stream classes.

OptionalDataExceptionException indicating the failure of an object read operation due to unread primitive data, or the end of data belonging to a serialized object in the stream.

StreamCorruptedExceptionThrown when control information that was read from an object stream violates internal consistency checks.

SyncFailedExceptionSignals that a sync operation has failed.

UncheckedIOExceptionWraps an IOException with an unchecked exception.

UnsupportedEncodingExceptionCharacter Encoding is not supported.

UTFDataFormatExceptionSignals that a malformed string in modified UTF-8 format has been read in a data input stream or by any class that implements the data input interface.

WriteAbortedExceptionSignals that one of the ObjectStreamExceptions was thrown during a write operation.

No comments:

Post a Comment