【发布时间】:2015-05-01 11:44:22
【问题描述】:
我要做的是将文本框中的信息保存到文本文件中。加载文本文件时,文本框将填满信息。保存文件时,我通过 Exception 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.");
}
}
【问题讨论】:
-
您能从
IOException ex获得更多信息吗? -
不只是“加载文件时出错”
-
您可以从
IOException获取更多信息,例如与getMessage... -
@Luke_i 请添加 e.printStackTrace();参与保存方法,并向我们展示结果
-
@Luke_i 所以您的 Customer 类根本不可序列化,您无法保存和加载,对吗,您没有实现可序列化?
标签: java javascript jquery serialization outputstream