我要做的是将文本框中的信息保存到文本文件中。加载文本文件时,文本框将填充信息。保存文件时,我通过异常e.printStackTrace()获取此错误;感谢
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();
} catch (IOException e) {
}
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.");
} catch (ClassNotFoundException ex) {
System.out.println("Invalid class in loaded file.");
}
}

答案 0 :(得分:1)
我认为您的Customer类不实现Serializable接口。将implements Serializable
添加到课程开始状态。