Meet the Serializable Java serialization this interface, we may have the following problems
A, what is the serialization and deserialization
B, role, why implement the Serializable interface, which is why the serialization
C, serialVersionUID this value is in how to set up, what's the use of, have a plenty of 1 l, have a plenty of a long string of Numbers, ing,
I just saw this keyword Serializable, just like the kind of question,
Before you deal with this problem, you need to know a problem, this is more important,
The Serializable interface, and related stuff, all in the inside of the Java IO,
1, the concept of serialization and deserialization
Serialization: the process of the object into a sequence of bytes is called object serialization,
Deserialization: the sequence of bytes recovery process is referred to as the object for deserialization,
Is specialized in the above explanation, now have some popular explanation, at the time of code to run, we can see a lot of objects (the debug build),
Can be a, also can is a kind of a collection of objects, a lot of object data, these data,
Some of the information we want him to be lasting saved, so the serialization,
Is to keep these objects inside the memory process described to become a string of bytes,
Is common to become file
I don't serialization can also save the file what ah, what is the effect? That's what I was asked,
2, what circumstances need to serialize
When you want to save the object state of memory to a file or database;
When you want to use the socket on the network transmission objects;
When you want to pass the RMI transfer objects;
(actually, the above several, I might have used a deposit database)
3, how to implement serialization
Implement the Serializable interface can
These theories above are simple, the actual code below to see the serialization can do, and will produce the bug problem,
On the first object code, flying pigs. Java
[Java] view plain copy
Package com. LXK. Model;
Import the Java. IO. Serializable.
/* *
* @ author LXK on 2017/11/1
*/
Public class FlyPig implements Serializable {
//private static final long serialVersionUID=1 l;
Private static String AGE="269";
Private String name;
Private String color;
Transient private String car;
//private String addTip.
Public String getName () {
Return the name;
}
Public void elegantly-named setName (String name) {
this.name=name;
}
Public String getColor () {
Return color;
}
Public void setColor (String color) {
This. Color=color;
}
Public String getCar () {
Return the car.
}
Public void setCar (String car) {
This. Car=car;
}
//public String getAddTip () {
//return addTip.
//}
//
//public void setAddTip (String addTip) {
//this. AddTip=addTip;
//}
@ Override
Public String toString () {
Return FlyPig {" + "
"Name='" + name +' \ '+'
"Color='" + color +,' \ ' '+
", car='" + car +' \ ' '+
", the AGE='" + AGE +' \ ' '+
//", addTip='" + addTip +' \ ' '+
'} ';
}
}
Pay attention to, the comments of the code, is a moment to use the various circumstances,
here is the main method?
[Java] view plain copy
Package com. LXK. Test;
The import com. LXK. Model. FlyPig;
Import the Java. IO. *;
/* *
* serialization test
*
* @ author LXK on 2017/11/1
*/
Public class SerializableTest {
Public static void main (String [] args) throws the Exception {
SerializeFlyPig ();
FlyPig FlyPig=deserializeFlyPig ();
System. The out. Println (flyPig. ToString ());
}
/* *
* serialization
*/
Private static void serializeFlyPig () throws IOException {
FlyPig FlyPig=new FlyPig ();
FlyPig. SetColor (" black ");
FlyPig. Elegantly-named setName (" naruto ");
FlyPig. SetCar (" 0000 ");
//ObjectOutputStream output stream object, will flyPig object storage to E disk flyPig. TXT file, complete the flyPig object serialization operation
The ObjectOutputStream oos=new ObjectOutputStream (new FileOutputStream (new File (" d:/flyPig. TXT ")));
Oos. WriteObject (flyPig);
System. Out.println (" FlyPig object serialization success!" );
Oos. The close ();
}
/* *
* deserialization
*/
Private static FlyPig deserializeFlyPig () throws the Exception {
ObjectInputStream ois=new ObjectInputStream (new FileInputStream (new File (" d:/flyPig. TXT ")));
FlyPig person=(FlyPig) ois. ReadObject ();
System. Out.println (" FlyPig object deserialization success!" );
Return the person;
}
}
For the above two operating instructions file stream class simple
The ObjectOutputStream represents the output stream object:
Its writeObject (Object obj) method for parameter specifies the obj Object serialization, to get the sequence of bytes to a target output flow,
ObjectInputStream representative object input stream:
Its readObject () method from the source of a sequence of bytes read from the input stream, deserialize them as an object, and returns it,
What specific operation,
Come up to the code, the first: don't move, run directly, see the effect,
Actual operation result, he will be in d:/flyPig. TXT generated file,
Seen from the results:
1, he realized the serialization and deserialization of objects,
2, transient modify attributes, is not be serialized, I set the audi four laps car disappear, became a null, my god,
3, you said don't try so hard, the static variable AGE also is serialized, the need to the other measurement,
The second: in order to test the static attributes can be serialization and deserialization, but the following,
[Java] view plain copy
Public static void main (String [] args) throws the Exception {
SerializeFlyPig ();
//FlyPig FlyPig=deserializeFlyPig ();
//System. Out. Println (flyPig. ToString ());
}
After this, meaning that is to say, first you serialize an object to a file, the object is the static with static variables,
Changes the value of the AGE in a class flyPig now give to 26,
Then, see below the operation code and execution result,
As you can see, the serialization of 269 just now, didn't read it, but just change of 26, if possible, should be covered the 26, 269,
So, come to the conclusion that the static static attributes, he doesn't serialization,
The third: demonstration function and usage of the serialVersionUID
Most of violence, and other direct the class implements the interface of the model are removed, and then perform the back of the serialization and deserialization methods, direct error,
Throw exceptions: NotSerializableException
This is too violent, do not recommend do,
And then, but also is similar to the above operations, first serialized method separately, generate documents,
Then open the properties addTip, after that, again deserialize method, see the phenomenon,
Throw exceptions: InvalidClassException details as follows,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull