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

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

}

最佳答案

我认为您的 Customer 类没有实现 Serializable 接口(interface)。添加 implements Serializable 到类(class)开场白。

关于java - 将文本框保存到文本文件并在文本框中加载文本文件内容(序列化)Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29986245/

相关文章:

javascript - 无需服务器端编码的文件操作

jquery - 使用 jquery 弹出内容

java - 如何使用java提取html表中特定td内的数据

java.sql.Timestamp 比较错误?

c# - 如何阅读 Lotus Notes 邮件存档 (*.nsf)

javascript - 外行术语中的 Unobtrusive Javascript 是什么?

javascript - 我可以动态渲染 NgModal 的 HTML吗

javascript - 使用javascript从 anchor 标记获取自定义数据

java - 记录 TestNG 测试用例

javascript - 将值从 RouterStateSnapshot 传递到 Angular 2 应用程序中的根路由文件