序列化是将对象写入到输出流中,用流存储或者传输。没实现哪个接口,你看看能不能把这个对象写入到流中。ObjectOutputStream和ObjectInputStream

解决方案 »

  1.   

    没有实现Serializable接口的话,你只能自己去完成这个动作了.你可以试试利用反射去读取实例中的字段,然后不管你是保存成XML还是JSON..还是二进制自定义格式都可以了.
      

  2.   

    你可以重新定义个新类,继承Person并且实现Serializable接口就可以了,之后使用新类就可以了,功能一点都不影响
    例如:public class Person_Serible implements Serializable{}然后使用Person_Serible就可以了
      

  3.   

    忘记继承了public class Person_Serible extends Person implements Serializable{}
      

  4.   

    若类 Person 未实现Serializable接口,可以肯定的说无法序列化Person的实例对象;
    不过你可以自己定义一个类实现Serializable接口, 并把新类的字段定义成和Person的实例字段完全一样,这样先序列化新类的实例,反序列化时再赋值给Person实例;示例:import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.Date;public class SerializableDemo { public static void main(String[] args) throws Exception {
    Person pers = new Person();
    pers.setName("Lee");
    pers.setAge(20);
    pers.setBirthday(new Date());

    PersonSerializable perSeri = new PersonSerializable();
    perSeri.copy(pers);

    ByteArrayOutputStream bou = new ByteArrayOutputStream();
    ObjectOutputStream oou = new ObjectOutputStream(bou);
    oou.writeObject(perSeri);

    ByteArrayInputStream bin = new ByteArrayInputStream(bou.toByteArray());
    ObjectInputStream oin = new ObjectInputStream(bin);
    PersonSerializable perSeriClone = (PersonSerializable)oin.readObject();

    System.out.println(perSeriClone.getName());
    System.out.println(perSeriClone.getAge());
    System.out.println(perSeriClone.getBirthday());


    System.out.println("======================");
    Person persClone = perSeriClone.copyToPerson();
    System.out.println(persClone.getName());
    System.out.println(persClone.getAge());
    System.out.println(persClone.getBirthday());
    }}class PersonSerializable implements Serializable {

    private static final long serialVersionUID = -8564152324839732089L;

    private String name;
    private int age;
    private Date birthday;

    public void copy(Person person){
    if (person == null) return;
    this.setName(person.getName());
    this.setBirthday(person.getBirthday());
    this.setAge(person.getAge());
    }

    public Person copyToPerson(){
    Person person = new Person();
    person.setName(this.getName());
    person.setBirthday(this.getBirthday());
    person.setAge(this.getAge());

    return person;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public int getAge() {
    return age;
    } public void setAge(int age) {
    this.age = age;
    } public Date getBirthday() {
    return birthday;
    } public void setBirthday(Date birthday) {
    this.birthday = birthday;
    }

    }class Person {
    private String name;
    private int age;
    private Date birthday;

    public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public int getAge() {
    return age;
    } public void setAge(int age) {
    this.age = age;
    } public Date getBirthday() {
    return birthday;
    } public void setBirthday(Date birthday) {
    this.birthday = birthday;
    }
    }
      

  5.   


    你说这个 我知道,但是怎么把 Person 和 Person_Serible 怎么转换是我头疼的地方
      

  6.   

    JsonMapper 序列化为json格式JsonMapper.nonEmptyMapper().toJson(object)
    JsonMapper.nonEmptyMapper().fromJson(jsonString, clazz)<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>${jackson.version}</version>
    </dependency>
      

  7.   

    第二种解法,OK了!!!public class Person {
    private String name;
    private int age;
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    }
    import java.io.IOException;
    import java.io.Serializable;public class Person_Serible extends Person implements Serializable {
    private static final long serialVersionUID = 1L; private void writeObject(java.io.ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();// 先序列化对象
    out.writeUTF(super.getName());// 再序列化父类的域
    out.writeInt(super.getAge());// 再序列化父类的域
    } private void readObject(java.io.ObjectInputStream in) throws IOException,
    ClassNotFoundException {
    in.defaultReadObject();// 先反序列化对象
    super.setName(in.readUTF());// 再反序列化父类的域
    super.setAge(in.readInt());// 再反序列化父类的域
    }
    }
    import java.io.*;
    /**
     * 测试类
     */
    public class Test { //对person实例序列化
    public static void serilizable(Person person, String writePath) throws IOException{
    FileOutputStream fileOutputStream = new FileOutputStream(new File(writePath));
    ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
    outputStream.writeObject(person);
    outputStream.close();
    }

    //反序列化
    public static Person unSerilizable(String filePath) throws ClassNotFoundException, IOException{
    FileInputStream fileInputStream = new FileInputStream(new File(filePath));
    ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
    Person person = (Person_Serible) inputStream.readObject();
    inputStream.close();
    return person;
    }

    public static void main(String[] args) {
    Person person = new Person_Serible();
    person.setName("zhangsan");
    person.setAge(15);

    try {
    Test.serilizable(person, "D:\\person.txt");//序列化为磁盘文件
    Person person2 = Test.unSerilizable("D:\\person.txt");//从磁盘文件读出来序列化文件
    System.out.println(person2.getName() + "\t" + person2.getAge());
    }  catch (ClassNotFoundException e) {
    e.printStackTrace();
    }catch (IOException e) {
    e.printStackTrace();
    }
    }
    }参见:http://www.yesky.com/376/1908876.shtml