Improve this answer. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 0. Related Any attempt to read object data which exceeds the boundaries of the custom data written by the corresponding writeObject method will cause an OptionalDataException to be thrown with an eof field value of true.
Non-object reads which exceed the end of the allotted data will reflect the end of data in the same way that they would indicate the end of the stream: bytewise reads will return -1 as the byte read or number of bytes read, and primitive reads will throw EOFExceptions. It is generally true of all the reading methods in this interface that if end of file is reached before the desired number of bytes has been read, an EOFException which is a kind of IOException is thrown.
Kentaro Shinbashi. Optional 'thank-you' note:. I've seen this topic many times before here, and I still have not found a solid answer to the problem: When you have a file that you've written many serialized objects to, and then you read in the objects, how do you know when you've read in all of the objects? Say you don't know exactly how many objects there are.
That's the first suggestion people usually come up with, and it's wrong. That's the second suggestion I've seen, but according to the API, it too is wrong. So, if I've got a file with a bunch of serialized objects in it, and I call readObject repeatedly to read in the objects, how do I know when I've reached the end of the file? Thank you. Edwin Dalorzo. Save the number of objects you are saving into the file at the beging of the file, and when you are deserializing the file, get that number back from the file.
When deserializing, ObjectInputStream implements ObjectInput, hence, you can read the number of objects using readInt or readLong methods. When writing the objects Yes, thank you Edwin. It is a good solution. Do you know about the case where the number of objects is not saved in the file? I should also say that EOFException seems to work, but it's undocumented, which makes me nervous to use it, since they might change things in Java 6 or 7 or whenever Joe Ess.
Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search.
The construction seems to be OK, but when I call the readObject method i get null instead of the object I expected. The file exists and the created BufferedInputStream reads the file correctly. Is the same version of Java being used on the Vista and XP boxes? Also check the version of Java used to create the xml originally.
If it fails on your XP machine, but works on your Vista machine, then that sounds like some environment problem. Check your system property file. It would be interesting to see if they're different. Does your. That means that somehow your InputStream is considered 'empty' by the XmlDecoder. Also, readObject throws ArrayIndexOutOfBoundsException if the stream contains no objects or no more objects , are you catching that exception?
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.
0コメント