Reputation: 8395
My Code-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectStreamExample {
/**
* @param args
*/
public static void main(String[] args) {
Person person = new Person();
person.setFirstName("Abhishek");
person.setLastName("Choudhary");
person.setAge(25);
person.setHouseNum(256);
ObjectOutputStream stream = null;
try {
stream = new ObjectOutputStream(new FileOutputStream(new File("Serialize.txt")));
stream.writeUTF(person.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(stream != null)
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ObjectInputStream input = null;
try {
input = new ObjectInputStream(new FileInputStream(new File("Serialize.txt")));
Person person2 = (Person) input.readObject();
System.out.println(person2.getFirstName());
System.out.println(person2.getLastName());
System.out.println(person2.getAge());
System.out.println(person2.getHouseNum());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
if(input != null)
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
and one Person bean file.
I am getting exception
java.io.OptionalDataException at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.readObject(Unknown Source) at com.practise.interview.nio.ObjectStreamExample.main(ObjectStreamExample.java:62)
This is getting raised because I think -
An attempt was made to read an object when the next element in the stream is primitive data. In this case, the OptionalDataException's length field is set to the number of bytes of primitive data immediately readable from the stream, and the eof field is set to false.
But how to avoid it as I know I set a primitive value, so way to avoid.?
Upvotes: 3
Views: 8915
Reputation: 9013
You are writing a String
and try to read a Person
. That's not how serialization works. In the context of serialization, an UTF string is considered primitive data, because it does not contain object information (class name, attributes etc.), but only the string data.
Write out the person
object itself, if you want to read a Person
afterwards:
stream.writeObject(person);
Addendum: If writing a String
would behave like with any other Object
, you would get a ClassCastException
instead, because the String
could not be cast to Person
. In any case, the mismatch between what you write and what you read is causing the error you got.
Upvotes: 6
Reputation: 41230
accroding to specification
Exception 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. This exception may be thrown in two cases:
An attempt was made to read past the end of data consumable by a class-defined readObject or readExternal method. In this case, the OptionalDataException's eof field is set to true, and the length field is set to 0.
stream.writeUTF(person.toString()); // Problem is here
Here you are write as UTF and in other end You are reading as a Person Object. You should change it -
stream.writeObject(person);
Upvotes: 1