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