Package Serialize;
/**
* Created by Hu on 2015/11/7.
*/
An interface that implements serialization must be implemented, which is an empty interface that plays the role of the identity
Import java.io.Serializable;
/**
* Classes used for serialization and deserialization
* */
public class Person implements Serializable {
private int age;
private String name;
Private String sex;
public person (int age, string name, string sex) {
This.age = age;
THIS.name = name;
This.sex = sex;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getsex () {
return sex;
}
public void Setsex (String sex) {
This.sex = sex;
}
@Override
Public String toString () {
Return "Person:name=" +name+ "sex=" +sex+ "age=" +age;
}
}
**************************************************************
Package Serialize;
Import java.io.*;
/**
* Created by Hu on 2015/11/7.
*/
Serialization and deserialization
public class Demo1 {
public static void Main (string[] args) throws exception{
for (int i=0;i<100;i++) {
Person Pp=new person (i, "Hujingwei" +i, "male");
Serializeperson (PP);
}
Deserializing only the first object in a sequence file
Person P=deserializeperson ();
SYSTEM.OUT.PRINTLN (P);
}
Serialization method that writes an object to a file on disk
public static void Serializeperson (person p) throws ioexception{
/*
* If the FileOutputStream constructor does not have a second argument "true", then the new object will be serialized to the file
* objects are overwritten, in which case there is always only one serialized object in the file, and when the second argument is set to "true", each time you add a
* The object is placed at the end of the file.
* */
ObjectOutputStream the object output stream, stores the person object in Person.txt, and completes the serialization operation on the Person object
ObjectOutputStream oo=new ObjectOutputStream (New FileOutputStream (New File ("D:/person.txt"), true));
Writes the serialized object to the input stream
Oo.writeobject (P);
System.out.println ("Serialize person successfully!");
Oo.close ();
}
Deserialization, reading an object from a disk file
public static person Deserializeperson () throws exception{
ObjectInputStream object input stream, which reads the person object from the file
ObjectInputStream oi=new ObjectInputStream (New FileInputStream (New File ("D:/person.txt"));
Person p= (person) oi.readobject ();
System.out.println ("Deserialize person successfully!");
return p;
}
}
********************************************************
Package Serialize;
/**
* Created by Hu on 2015/11/7.
*/
Import java.io.*;
Import java.util.ArrayList;
Import java.util.List;
/**
* Serialization and deserialization of multiple objects
* */
public class Demo2 {
public static void Main (string[] args) throws exception{
Serialize ();
List list=deserialize ();
for (int i=0;i<list.size (); i++) {
Person p= (person) list.get (i);
SYSTEM.OUT.PRINTLN (P);
}
}
Serialization methods
public static void Serialize () throws exception{
Saves multiple objects in a list and then stores the list as an object in a file (list already implements the Serializable interface)
list<person> list= new arraylist<person> ();
for (int i=0;i<100;i++) {
Person P=new person (i, "Test" +i, "male");
List.add (P);
}
ObjectOutputStream oo=new ObjectOutputStream (New FileOutputStream (New File ("D:/person.txt"), true));
Oo.writeobject (list);
Oo.close ();
}
Deserialization method
public static List deserialize () throws exception{
ObjectInputStream oi=new ObjectInputStream (New FileInputStream (New File ("D:/person.txt"));
List list= (list) oi.readobject ();
return list;
}
}
Serialization and deserialization in Java, including serialization of multiple objects into a single file