我正在保存一个将Person类扩展为文本文件的Customer。详细信息写在不同的文本框中,例如客户的姓名和姓氏。保存文本文件时,该文件不可读,但结尾处有详细信息。所以我仍然不知道它是否应该以这种方式保存...假设它被正确保存,我正在尝试加载保存的文件并用正确的内容填写文本框,例如Name和Surname of顾客。但是当加载文件时,我没有任何错误,就像什么也没发生或点击一样。因为我是新手并且不知道我做得不对,需要一些帮助。
private void savecustButtonActionPerformed(java.awt.event.ActionEvent evt) {
Customer customer = new Customer();
try {
FileOutputStream fos = new FileOutputStream("Customers/" + custidTF.getText() + ".txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
customer.setPersonName((custnameTF.getText()));
customer.setPersonSurname((custsurnameTF.getText()));
customer.setPersonID((custidTF.getText()));
oos.writeObject(customer);
oos.close();
fos.close();
custnameTF.setText("");
custsurnameTF.setText("");
custidTF.setText("");
JOptionPane.showMessageDialog(this, "Person was Saved Successfully!",
"Success", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Person could not be Saved!",
"Error!", JOptionPane.INFORMATION_MESSAGE);
dispose();
}
private void loadCustomerActionPerformed(java.awt.event.ActionEvent evt) {
Customer customerfile = null;
try {
final JFileChooser chooser = new JFileChooser("Customers/");
int chooserOption = chooser.showOpenDialog(null);
chooserOption = JFileChooser.APPROVE_OPTION;
File file = new File(chooser.getSelectedFile().getAbsolutePath());
ObjectInputStream in = new ObjectInputStream(
new FileInputStream(file)
);
customerfile = (Customer) in .readObject();
in .close();
} catch (IOException ex) {
System.out.println("Error loading file." + ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println("Invalid class in loaded file.");
}
}
public class Person implements Serializable {
private String personName;
private String personSurname;
private String personID;
private String personTel;
private String personMob;
public Person() {
}
/**
* @return the personName
*/
public String getPersonName() {
return personName;
}
/**
* @param personName the personName to set
*/
public void setPersonName(String personName) {
this.personName = personName;
}
/**
* @return the personSurname
*/
public String getPersonSurname() {
return personSurname;
}
/**
* @param personSurname the personSurname to set
*/
public void setPersonSurname(String personSurname) {
this.personSurname = personSurname;
}
/**
* @return the personID
*/
public String getPersonID() {
return personID;
}
/**
* @param personID the personID to set
*/
public void setPersonID(String personID) {
this.personID = personID;
}
public class Customer extends Person implements Serializable {
private String consultant;
/**
* @return the consultant
*/
public String getConsultant() {
return consultant;
}
/**
* @param consultant the consultant to set
*/
public void setConsultant(String consultant) {
this.consultant = consultant;
}
}