带有序列化的Java OOP,文件未加载

时间:2015-08-14 06:24:56

标签: java oop

我正在尝试创建一个表单,用户可以保存三条信息(id,name和surname)。以下是该人的代码:

public class Person implements Serializable {

private String Personfirstname;
private String Personlastname;
private String PersonID;

/**
 * @return the Personfirstname
 */
public String getPersonfirstname() {
    return Personfirstname;
}

/**
 * @param Personfirstname the Personfirstname to set
 */
public void setPersonfirstname(String Personfirstname) {
    this.Personfirstname = Personfirstname;
}

/**
 * @return the Personlastname
 */
public String getPersonlastname() {
    return Personlastname;
}

/**
 * @param Personlastname the Personlastname to set
 */
public void setPersonlastname(String Personlastname) {
    this.Personlastname = Personlastname;
}

/**
 * @return the PersonID
 */
public String getPersonID() {
    return PersonID;
}

/**
 * @param PersonID the PersonID to set
 */
public void setPersonID(String PersonID) {
    this.PersonID = PersonID;
}  


 public void savecons()
{
    try {
        File selectedFile = new File("Consultant - " + PersonID + ".txt");
        FileOutputStream fileStream = new FileOutputStream(selectedFile);
        ObjectOutputStream oos = new ObjectOutputStream(fileStream);
        oos.writeObject(this);
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}

private String toString(int ConsultantID) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}  


public static Person loadcons() throws Exception
{
    Person loadcons = null;

        JFileChooser chooser = new JFileChooser();
        int chooserOption = chooser.showSaveDialog(null);
        chooserOption = JFileChooser.APPROVE_OPTION;

        try {
            File file = new File (chooser.getSelectedFile().getAbsolutePath());
            ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));


        loadcons = (Person) input.readObject();
            input.close();
       return loadcons;
   } catch (IOException ex) {
        System.out.println(ex.getMessage());
    } catch (ClassNotFoundException ex) {
        System.out.println(ex.getMessage());
    }
   throw new Exception("No files were selected");
}

private String toString(String PersonID) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

代码有3个变量和2个方法。其中一种方法将变量信息保存到文本文件中(文本文件正在输出,但我不确定信息是否作为其所有符号进入。另一种方法是加载按钮,将数据导入回来田野。

然后我使用以下代码创建了一个表单。保存是:

 Person cons_save = new Person();
    cons_save.setPersonfirstname(this.jTextField1.getText());
    cons_save.setPersonlastname(this.jTextField2.getText());
    cons_save.setPersonID(this.jTextField3.getText());
    this.jTextField1.setText("");
    this.jTextField2.setText("");
    this.jTextField3.setText("");

    cons_save.savecons();

,加载如下:

Person cons_load = Person.loadcons();
        this.jTextField1.setText(cons_load.getPersonfirstname());
        this.jTextField2.setText(cons_load.getPersonlastname());
        this.jTextField3.setText(cons_load.getPersonID());

当我按下加载按钮时,它不起作用,因为它需要一个例外,但是当我创建例外按钮时,按钮可以工作,但是当我选择文件时,信息不会进入字段。

 Person cons_load;
    try {
        cons_load = Person.loadcons();
        this.jTextField1.setText(cons_load.getPersonfirstname());
        this.jTextField2.setText(cons_load.getPersonlastname());
        this.jTextField3.setText(cons_load.getPersonID());
    } catch (Exception ex) {
        Logger.getLogger(CreateConsultant.class.getName()).log(Level.SEVERE, null, ex);
    }

我感谢我能得到的每一个帮助,因为这是我第一次尝试用java oop编程。

1 个答案:

答案 0 :(得分:2)

您需要使用Serializable界面将要保存的类标记为文件。这应该允许你所追随的对象的序列化。

根据JavaDoc(我用粗体突出显示了一些文字):

  

类的可序列化由实现该类的类启用   java.io.Serializable接口。 不实现此功能的类   interface不会将其任何状态序列化或反序列化。   可序列化类的所有子类型本身都是可序列化的。该   序列化接口没有方法或字段,仅用于   确定可序列化的语义。

基本上,这个:public class Person需要成为:public class Person implements Serializable { static final long serialVersionUID = ...

重要的是serialVersionUID对每个类都是唯一的,因为它用于序列化和反序列化。

编辑:根据下面的评论,我复制了你的代码并运行它。我设法保存并回读没有问题。代码按照您的问题运行,看到您已添加标记界面(最好还包括您的serialVersionUID字段)。

然后我删除了代码的implements Serializable部分,我收到了此错误:writing aborted; java.io.NotSerializableException: so.Person。这基本上表明您正在尝试存储不可序列化的项目。

以下是抛出异常时文件的内容:

Error thrown during serialization of non serializable item.

相关问题