During the java program running process, many objects exist at the same time, but these objects disappear when the program ends or the JVM stops running. How can we save these objects so that they can be read into the memory next time? Or how to upload some objects to the java program at the other end through the network? This operation for implementing objects is called object serialization (or persistence), and re-reading the memory is called deserialization.
The packaging class of the basic data type and all container classes can be serialized. Custom classes cannot be serialized by default. If you want to serialize a defined class, you must implement the java. io. Serializable interface for this class.
Here is a Demo:
Point z; Point( x, y, .x = .y = .z = "(" + x + "," + y + "," + z + ")" main(String[] args) String fileName = "F:\\shar\\test\\test7.txt" ObjectOutputStream oos = ObjectOutputStream( ( i = 0; i < 10; i++ oos.writeObject( Point(i,2*i,3* ObjectInputStream ois = ObjectInputStream( ( i = 0; i < 10; i++ Point p = System.out.println(p + " " }
Running result:
(0, 0)
(1, 2, 0)
(2, 4, 0)
(3, 6, 0)
(4,8, 0)
(5, 10, 0)
(6, 12, 0)
(7, 14, 0)
(8, 16, 0)
(9, 18, 0)
The running results show that the z attributes of all Piont class objects are not serialized. It should have been modified by a keyword transient. The attributes modified by this keyword do not participate in persistence.