如何序列化/反序列化java中的对象列表?

时间:2013-02-01 22:13:50

标签: java class serialization deserialization

如何在Java中序列化Serializable类的许多对象?

例如

public class Personne implements java.io.Serializable {

    String nom;
    String date;

    Personne(String nom,String date)
    {
        this.nom=nom;
        this.date=date;
    }
}

我想制作许多物品:

Personne p1 = new  Personne("X","XX");
Personne p2 = new  Personne("Y","ZZ");
Personne p3 = new  Personne("Z","ZZ");

- >用于序列化这些对象的静态函数

- >用于反序列化的静态函数

完整代码:

package game2;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 
 * @author ahmedJ
 */
public class Personne implements java.io.Serializable {

    String nom;
    String date;

    Personne(String nom, String date) {
        this.nom = nom;
        this.date = date;
    }

    public static void Serialiser(Personne sp) {

        try {
            ObjectOutputStream out = new ObjectOutputStream(
                    new FileOutputStream("Score.sp"));
            out.writeObject(sp);
            out.flush();
            out.close();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
    }

    public static void Deserialiser() {

        Personne a1, a2, a3;
        try {
            FileInputStream file = new FileInputStream("Score.sp");
            ObjectInputStream in = new ObjectInputStream(file);
            a1 = (Personne) in.readObject();
            a2 = (Personne) in.readObject();
            a3 = (Personne) in.readObject();

            System.out.println(a1.nom);
            System.out.println(a2.nom);
            System.out.println(a3.nom);

            in.close();
            file.close();
        } catch (Exception ex) {
            System.err.println("Erreur de lecture " + ex);
        }

    }

    public static void main(String[] sd) {
        Personne p1 = new Personne("X", "XX");
        Personne p2 = new Personne("Y", "ZZ");
        Personne p3 = new Personne("Z", "ZZ");

        Serialiser(p1);
        Serialiser(p2);
        Serialiser(p3);

        Deserialiser();
    }
}

2 个答案:

答案 0 :(得分:9)

使用ObjectOutputStream撰写,ObjectInputStream阅读。很简单,只是工作:

  ObjectOutputStream out = new ObjectOutputStream(myFileOutputStream);
  out.writeObject(p1);
  out.writeObject(p2);
  out.writeObject(p3);
  out.close();
  ....
  ObjectInputStream in = new ObjectInputStream(myFileInputStream);
  p1 = (Personne) in.readObject();
  p2 = (Personne) in.readObject();
  p3 = (Personne) in.readObject();

答案 1 :(得分:2)

您可以向Personne添加静态方法,如此。

public class Personne implements Serializable{
//Other stuff here
    public static void writePersonne(ObjectOutputStream out,Personne p){
        try{
          out.writeObject(p);
          out.flush();
        }catch(Exception e){
           e.printStackTrace();
        }
    }
    public static Personne readPersonne(ObjectInputStream in){
        try{
          return (Personne)in.readObject();
        }catch(Exception e){
           e.printStackTrace();
        }
    }

你只需迭代一个数组或某种类型的集合并写下每一个,然后在另一边读取它们。