-
Deserialization
Any ideas on how i could go about deserializing SerObj?
Code:
import java.io.*;
class SerObj implements Serializable{
transient private int temp;
private boolean indicator;
public SerObj(int temp, boolean indicator){
this.temp = temp;
this.indicator = indicator;
}
public int getTemp(){
return temp;
}
public boolean getIndicator(){
return indicator;
}
}
public class X{
public static void main(String[] args){
ObjectOutputStream oup = null;
try{
oup = new ObjectOutputStream(new FileOutputStream("C:" + File.separator + "ObjSerFile"));
oup.writeObject(new SerObj(68,true));
}catch(IOException io){
System.err.println(io);
}finally{
try{
oup.close();
}catch(IOException io){
System.err.println(io);
}
}
}
}
-
I have this but a java.io.EOFException is thrown.
Code:
import java.io.*;
public class Y{
public static void main(String[] args){
ObjectInputStream oip = null;
try{
oip = new ObjectInputStream(new FileInputStream("C:" + File.separator + "ObjSerFile"));
int temp = oip.readInt();
boolean indicator = oip.readBoolean();
SerObj serobj = new SerObj(temp, indicator);
System.out.println(serobj.getTemp());
System.out.println(serobj.getIndicator());
}catch(IOException io){
System.err.println(io);
}finally{
try{
oip.close();
}catch(IOException io){
System.err.println(io);
}
}
}
-
SerObj so = (SerObj)oip.readObject();
There are two problems with your approach. First, classes are specially encoded, so you can't just read the members back. Second, temp is marked as transient and thus doesn't get saved, so even if you could read the members back, there wouldn't be anything to read.
-
:thumb: Ok works fine. Thanks.
Code:
import java.io.*;
public class Y{
public static void main(String[] args){
ObjectInputStream oip = null;
try{
oip = new ObjectInputStream(new FileInputStream("C:" + File.separator + "ObjSerFile"));
SerObj serobj = (SerObj)oip.readObject();
System.out.println(serobj.getTemp());
System.out.println(serobj.getIndicator());
}catch(Exception io){
System.err.println(io);
}finally{
try{
oip.close();
}catch(IOException io){
System.err.println(io);
}
}
}
}