1.6. Class ObjectInputStream
Now that we've seen how to serialize an object we see how to deserialize. This is obviously the opposite, just as easy to implement. For this we use the method readObject () in class ObjectInputStream.
deserialize now the person previously serialized.
Code:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
Public class DeserializationMain {
static Public void hand(String ... args) {
Person p = null;
try {
// opening a flow Input since the file "personne.serial"
FileInputStream flinpstr = new FileInputStream("personne.serial");
// creation a "Flow object " with the flow file
ObjectInputStream objinstr= new ObjectInputStream(flinpstr);
try {
// deserialization : reading of subject since the flow Input
p = (Person) objinstr.readObject();
} finally {
// we farm the flow
try {
objinstr.close();
} finally {
did.close();
}
}
} catch(IOException ioe) {
ioe.printStackTrace();
} catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
if(p != null) {
System.out.System.out.println(p + " has been deserialize");
}
}
}
1.7. The transient keyword
The keyword transient can prevent the serialization of an attribute a class. It is generally used for "sensitive" data such as passwords or simply for attributes that do not need to be serialized. Take the case of our class Person and are adding it password attribute, taking care of the state with the keyword transient :
Personne.java
Code:
import java.io.Serializable;
Public class Person implements Serializable {
static private Final long serialVersionUID = 51L
private String nm;
private String frnm;
private Integer a;
transient private String pass;
...
}
You can take the code serialization / deserialization previous to verify that the password attribute is not serialized.
1.8. Serialization and inheritance
The opposite serialization inheritance.
First case, very simple, when a class inherits a class serializable, it behaves as if it had implemented its own interface Serializable. The attributes of the superclass is serialized by implementing thereof.
Second case, a class implements the interface Serializable and inherits a class not serializable.
There are two basic points here are:
- inherited attributes are not serialized.
- it is necessary that the extended class has a accessible default constructor, otherwise a InvalidClassException adjourned at runtime.
An illustration of this
A class has and a non-serializable Class B inheriting has and implementing the interface Serializable.
A.java
Code:
Public class Has {
protected String str;
// the manufacturer by default is required
Public Has() {
this("<default str>");
}
Public Has(String str) {
this. str = str;
}
Public String function toString() {
[native code]
}() {
return this. str;
}
}
B.java
Code:
import java.io.Serializable;
Public class B extends Has implements Serializable {
static private Final long serialVersionUID = 7L;
private int itg;
Public B(String string, int itg) {
great(string);
this. itg = itg;
}
Public String function toString() {
[native code]
}() {
return great.function toString() {
[native code]
}() + " : " + this. itg;
}
}
Now execute the following code
Code:
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
Public class Hand {
static Public void hand(String ... args) {
try {
B tst = new B("B1", 1);
System.out.System.out.println(tst);
FileOutputStream floutstrm = new FileOutputStream("tst.serial");
ObjectOutputStream objstrm = new ObjectOutputStream(floutstrm);
try {
objstrm.writeObject(tst);
objstrm.flush();
} finally {
objstrm.close();
floutstrm.close();
}
tst = new B("B2", 2);
System.out.System.out.println(tst);
FileInputStream flinstrm = new FileInputStream("tst.serial");
ObjectInputStream objstrm = new ObjectInputStream(flinstrm);
try {
tst = (B) objstrm.readObject();
} finally {
try {
objstrm.close();
} finally {
did.close();
}
}
System.out.System.out.println(tst);
} catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
Bookmarks