【问题标题】:Save Text Boxes to Text File and Load Text File Content in Textboxes (Serialized) Java将文本框保存到文本文件并在文本框(序列化)Java 中加载文本文件内容
【发布时间】: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


【解决方案1】:

我认为您的 Customer 类没有实现 Serializable 接口。在开课声明中添加implements Serializable

【讨论】:

  • 我用实现 Serializable 编辑了我的类,保存文件时我现在没有错误:),但文件仍然没有加载。谢谢
  • 如果您尝试从客户 customerfile 文本字段中读取数据会怎样?
  • 从客户 customerfile 文本字段中读取数据是什么意思? textfileds 中的文本被保存到文本文件中,而不是在加载时给出 Excpetion 错误。我想加载对象。
  • 再次测试负载后,它没有给出任何错误。即使使用 e.printStackTrace();文本框仍然是空的。谢谢
  • 但是GUI中的文本框,还是customerfile的字段?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 2012-04-18
  • 2015-05-18
  • 2021-10-12
  • 1970-01-01
  • 2012-03-20
相关资源
最近更新 更多