When will an EOFException occur in JAVA's streams?

Up vote 4 down vote favorite share g+ share fb share tw.

I am working with a DataInputStream and had a question about EOFExceptions. According to java docs: Signals that an end of file or end of stream has been reached unexpectedly during input. This exception is mainly used by data input streams to signal end of stream.

Note that many other input operations return a special value on end of stream rather than throwing an exception. Does this mean that when a EOFException is generated, the stream will not NEVER open again? Does it mean you should NEVER expect any more data from it ever?

If an outputstream is connected to an inputstream and outputstream.close() is called, will an inputstream receive the EOFException or an IOException? An IOException is described as: Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.

Does a close on the outputstream produce either a EOFException or an IOException on the datainputstream side? Java input output stream eofexception link|improve this question edited Mar 19 '09 at 22:48GEOCHET11.1k93258 asked Mar 19 '09 at 22:38jbu1,54742555 22% accept rate.

The key word is unexpected. If you use DataInputStream and read a 4 byte integer but there were only 3 bytes remaining in the stream you'll get an EOFException. But if you call read() at the end of stream you'll just get -1 back and no exception.

Answering another part of your question: Yes, EOF means that no more data will be seen on the stream; you should close it.

EOFException is a subclass of IOException. It will be thrown if you attempt to read from the stream and there is no more data to be read (e.g. Because your DataInputStream is wrapped around a FileInputStream and you're trying to read more bytes than are left in the file).

In other words, just because I read and don't see any data doesn't mean the file (in a file input stream) has ended. It may just be that the bytes haven't arrived yet. I don't see the FileInputStream returning EOF in docs.

– jbu Mar 19 '09 at 23:05 With a file, you know when you've reached the end. The wrapped FileInputStream would throw an EOFException, which is propagated by the DataInputStream to your code. – Simon Nickerson Mar 20 '09 at 8:42.

EOFException is thrown 1. If there is no data in a STREAM but you are trying to read...eg read methods of chain streams like DataInputStream, ObjectInputStream, RandomAccessFile throw EOFException if they are trying to read from FileInputStream but the FileInputStream is empty 2. Or if the formats are not matching...eg if int is present and you are using readFloat() of DataInputStream.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions