2015-08-14 16 views
0

我想创建一个表单,用户保存三条信息(id,name和surname)。以下是该人的代码:带序列化的Java OOP,文件没有被加载

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()); 

当我按下加载按钮,它不起作用,因为它需要一个例外,但是当我创建ex时ception按钮的作品,但是当我选择文件时,信息不会出现在字段中。

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。

+0

所以,你会得到一个例外,但是不知道为什么。然后发布异常堆栈跟踪。它告诉你为什么。我的猜测是,异常是由于您没有关闭ObjectOutputStream而导致的。哦,正如npinti正确地提到的那样:你的课必须是可序列化的。不要捕获savecons()中的IOException:它隐藏了该错误。并尊重Java命名约定。并为您的方法提供有意义的名称。 savecons是什么意思? –

回答

2

您需要将要保存的类标记为Serializable界面。这应该允许您之后的对象的序列化。

按照JavaDoc中(我用粗体突出显示一些文本):

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

从本质上讲,这一点: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.

+0

所以我标记了将Serializable作为接口实现的人,但仍然存在相同的问题。但是,谢谢这一点,我忘了加入。我将编辑我的帖子 –

+0

这也是我的答案。我测试了提供的代码,并想知道为什么序列化工作,即使'Person'类不是'Serializable'?该文件实际上创建!我本来期望将Person对象保存到文件的例外情况 – beosign

+1

@beosign OP会捕获IOException并基本上忽略它。我认为在文件中看到的是一旦ObjectOutputStream被创建就写入的序列化头。 –