Serialization and deserialization in Java, including serialization of multiple objects into a single file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.