我要做的是将文本框中的信息保存到文本文件中。加载文本文件后,文本框将填满信息。保存文件时,我通过异常 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.");
}
}
最佳答案
我认为您的 Customer 类没有实现 Serializable 接口(interface)。添加 implements Serializable
到类(class)开场白。
关于java - 将文本框保存到文本文件并在文本框中加载文本文件内容(序列化)Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29986245/