将文本框保存到文本文件并在文本框中加载文本文件内容(序列化)Java

时间:2015-05-01 11:44:22

标签: java javascript jquery serialization outputstream

我要做的是将文本框中的信息保存到文本文件中。加载文本文件时,文本框将填充信息。保存文件时,我通过异常e.printStackTrace()获取此错误;感谢

enter image description here



 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.");
  }

}




1 个答案:

答案 0 :(得分:1)

我认为您的Customer类不实现Serializable接口。将implements Serializable添加到课程开始状态。