Java - 自定义类的NotSerializableException

时间:2015-02-04 08:54:22

标签: java ioexception

我在Java中使用此代码(对不起,如果我使用西班牙语中的某些术语)。一切都没问题,直到我需要在ObjectOutputStream printStackTracejava.io.NotSerializableException的情况下在package agenda; import java.io.*; public class Agenda implements java.io.Serializable { public static void addContact() { String nom, ap1, ap2, tf, em; Contact newContact = new Contact("Nombre", "Apellido", "Apellido 2", "Telefono", "Email"); try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("agenda.pzs", true)) { oos.writeObject(newContact); // NetBeans says the error is here } catch (IOException ex) { ex.printStackTrace(); // This is provisional only System.out.println("Ocurrió un error inesperado."); } } 的文件中写一个对象。

代码就是这个。

{{1}}

抱歉我的英语不好。

2 个答案:

答案 0 :(得分:0)

public class Agenda implements java.io.Serializable {

    public static void addContact() {

    String nom, ap1, ap2, tf, em;

    Contact newContact = new Contact("Nombre", "Apellido", "Apellido 2", "Telefono", "Email");

    try {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("agenda.txt",true));
            oos.writeObject(newContact);
            oos.close();            

    } catch (IOException ex) {
        ex.printStackTrace(); 
        System.out.println("Ocurrió un error inesperado.");
    } 

    }
}

您的括号出现错误

并在Contact class中实现Serializable接口

答案 1 :(得分:0)

Serializable类中的所有对象都必须是Serializable。这个要求是可以遍历的 - 下面的所有对象也必须是Serializable ..

... BUT 假设Contact是第三方类,或者由于任何其他原因不允许您修改它,可以采用以下解决方案:

免责声明:这很棘手,如果您不熟悉“联系”课程,可能会导致许多意外问题。您必须确保能够使用Serializable块读取/写入“Contact”类的状态。必须跟踪Contact实现中的任何更改。您需要一组JUnit测试来验证包装器是否正确。

现在谈谈: 创建ContactWrapper类,它将“Contact”类反汇编为write()上的可序列化类,并在read()期间从块中组装此类。或多或少这样的事情:

import static org.junit.Assert.*;

import java.io.*;

import org.junit.Test;

public class Agenda {

@Test
public void addContact() throws ClassNotFoundException, FileNotFoundException, IOException {

    String nom, ap1, ap2, tf, em;

     Contact newContact = new Contact("Nombre", "Apellido", "Apellido 2", "Telefono", "Email");

     ContactWrapper wrapper = new ContactWrapper();
     wrapper.setContact(newContact);

     ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(new FileOutputStream("agenda.pzs"));
        oos.writeObject(wrapper);
    } finally{
       if(oos != null){ 
           oos.close();
       }
    }

    ObjectInputStream ois =  null;
    ContactWrapper wrapper_new = null;
    try{
        ois = new ObjectInputStream(new FileInputStream("agenda.pzs"));
        wrapper_new = (ContactWrapper) ois.readObject();
    } finally {
        if(ois != null)
            ois.close();
    }
    assertTrue(wrapper.equals(wrapper_new));

    Contact deserializedContact = wrapper.getContact();
    //Assuming that contact has corectly implemented equals()...
    assertTrue(newContact.equals(deserializedContact));

}
}

=============================================== =

import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.Serializable;

public class ContactWrapper implements Serializable{
//assume not nulls here...-->equals
private String nom, ap1, ap2, tf, em;

public ContactWrapper(){

}

public void setContact(Contact contact){
  //Get State of "Contact" and write it to Serializable objects
  nom = contact.getNom();
  ap1 = contact.getAp1();
  //etc....
}

public Contact getContact(){
    //create Contact from serialized objects
    return new Contact(nom, ap1, ap2, tf, em);
}

private void writeObject(java.io.ObjectOutputStream out)
        throws IOException{
    System.out.println("write");
    out.writeObject(nom);
    out.writeObject(ap1);
    out.writeObject(ap2);
    out.writeObject(tf);
    out.writeObject(em);

}

    private void readObject(java.io.ObjectInputStream in)
        throws IOException, ClassNotFoundException{
        System.out.println("read");   
        nom = (String) in.readObject();
        ap1 = (String) in.readObject();
        ap2 = (String) in.readObject();
        tf = (String) in.readObject();
        em = (String) in.readObject();

    }

    private void readObjectNoData()
        throws ObjectStreamException{
        System.out.println("readNoData");
        //set everything to null?
    }

    @Override
    public boolean equals(Object other) {
        if(this == other) return true;
        if(other instanceof ContactWrapper){
            ContactWrapper that = (ContactWrapper)other;
            if(this.nom.equals(that.nom) &&
                    this.ap1.equals(that.ap1)
                    //...etc etc
                    ){
                return true;
            }
        }
        return false;
    }
}

编辑: 资料来源:http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html