Puspender Tanwar wrote:
this code is from OCP7 programmer certification book by mala gupta. Only path i and class names are changed.
but my code is giving EOFException. Can someone explain why ? I am only writing to the file, still EOFException
Henry Wong wrote:I will speculate that the original code didn't have the same file for both output and input.
Henry Wong wrote:The file outputstream is causing the file to be zero length. And hence, the inputstream doesn't have anything to read... so when the object inputstream tried to read the header, there is nothing to read, hence EOF.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:You are initializing the ObjectInputStream before the ObjectOutputStream. So when the ObjectInputStream is created the file doesn't exist. Fix that to match the order of initialization in the book and you'll get a successful run...
javadoc wrote:Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:...So if you provide an empty file to ObjectInputStream constructor then you get EOFException as it's not able to find the header....
Ankit Garg wrote:It seems like the ObjectOutputStream constructor writes some initial bytes (header) to the file which the ObjectInputStream constructor looks for (as the method name ObjectInputStream.readStreamHeader in the stack trace suggests). So if you provide an empty file to ObjectInputStream constructor then you get EOFException as it's not able to find the header.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:<Assumption value="You know you have changed ObjectInputStream to DataInputStream">
The documentation of constructor of DataInputStream doesn't say it fetches any header from the input file. So no exception in case of DataInputStream...
</Assumption>
A serialization stream header is read from the stream and verified.
I still not got why ObjectInputStream throw exception and what if it doesn't have made to do so ?
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:Puspender, the constructor of the ObjectInputStream class reads the input file and looks for some headers to check if it's a valid file i.e. it's a file which contains serialized objects. That's what this line from the documentation of the constructor mean
Ankit Garg wrote:I didn't understand the last part of your question...
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Puspender wrote:
Ankit Garg wrote:Puspender, the constructor of the ObjectInputStream class reads the input file and looks for some headers to check if it's a valid file i.e. it's a file which contains serialized objects. That's what this line from the documentation of the constructor mean
and ObjectOutputSrtream guarantee that now the file can store the serialized objects or it may have such objects. That's why no exception if ObjectOutputSrtream is written first . Am i right ?
Consider Paul's rocket mass heater. |