我在做的是將文本框中的信息保存到文本文件中。當文本文件被加載時,文本框將被填充信息。當保存文件時,我通過異常e.printStackTrace()得到這個錯誤;由於將文本框保存到文本文件並加載文本框中的文本文件內容(序列化)Java
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'](https://docs.oracle.com/javase/7/docs/api/java/io/IOException.html)獲得更多信息,例如與'getMessage' ... –