找到你要的答案

Q:Load Serialized File content int Text Boxes

Q:负载序列化的文件内容为文本框

I am saving a Customer that extends a Person class into a text file. The details are written in different text boxes such as the Name and Surname of the Customer. When the text file is saved, the file is not readable but and the end the details are there. So I still don't know if its supposed to be saved this way... Assuming that its being saved correctly, I am trying to load the file saved and fill up the text boxes with the right content such as the Name and Surname of the customer. But when loading the file I get no Errors whatsoever like nothing happened or clicked. Need a little bit of help since I'm new and don't know what I'm not doing right.

 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();
       fos.close();

       custnameTF.setText("");
       custsurnameTF.setText("");
       custidTF.setText("");

       JOptionPane.showMessageDialog(this, "Person was Saved Successfully!",
         "Success", JOptionPane.INFORMATION_MESSAGE);

     } catch (IOException e) {
       JOptionPane.showMessageDialog(this, "Person could not be Saved!",
         "Error!", JOptionPane.INFORMATION_MESSAGE);

       dispose();

     }

loadCustomerActionPerformed

 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." + ex.getMessage());
   } catch (ClassNotFoundException ex) {
     System.out.println("Invalid class in loaded file.");
   }

 }

Person

public class Person implements Serializable {

  private String personName;
  private String personSurname;
  private String personID;
  private String personTel;
  private String personMob;

  public Person() {

    }
    /**
     * @return the personName
     */
  public String getPersonName() {
    return personName;
  }

  /**
   * @param personName the personName to set
   */
  public void setPersonName(String personName) {
    this.personName = personName;
  }

  /**
   * @return the personSurname
   */
  public String getPersonSurname() {
    return personSurname;
  }

  /**
   * @param personSurname the personSurname to set
   */
  public void setPersonSurname(String personSurname) {
    this.personSurname = personSurname;
  }

  /**
   * @return the personID
   */
  public String getPersonID() {
    return personID;
  }

  /**
   * @param personID the personID to set
   */
  public void setPersonID(String personID) {
    this.personID = personID;
  }

Customer

public class Customer extends Person implements Serializable {

  private String consultant;

  /**
   * @return the consultant
   */
  public String getConsultant() {
    return consultant;
  }

  /**
   * @param consultant the consultant to set
   */
  public void setConsultant(String consultant) {
    this.consultant = consultant;
  }

}

我正在保存将一个人类扩展为文本文件的客户。详细信息写在不同的文本框,如客户的姓名和姓氏。当文本文件被保存,文件是不可读的,但最终的细节有。所以我仍然不知道它是否应该这样保存…假设它被正确保存,我试图加载保存的文件,并填写正确的内容,如客户的姓名和姓氏的文本框。但加载文件时,我没有得到任何错误,就像没有发生或点击。需要一点帮助,因为我是新的,不知道我做的不对。

 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();
       fos.close();

       custnameTF.setText("");
       custsurnameTF.setText("");
       custidTF.setText("");

       JOptionPane.showMessageDialog(this, "Person was Saved Successfully!",
         "Success", JOptionPane.INFORMATION_MESSAGE);

     } catch (IOException e) {
       JOptionPane.showMessageDialog(this, "Person could not be Saved!",
         "Error!", JOptionPane.INFORMATION_MESSAGE);

       dispose();

     }

loadCustomerActionPerformed

 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." + ex.getMessage());
   } catch (ClassNotFoundException ex) {
     System.out.println("Invalid class in loaded file.");
   }

 }

Person

public class Person implements Serializable {

  private String personName;
  private String personSurname;
  private String personID;
  private String personTel;
  private String personMob;

  public Person() {

    }
    /**
     * @return the personName
     */
  public String getPersonName() {
    return personName;
  }

  /**
   * @param personName the personName to set
   */
  public void setPersonName(String personName) {
    this.personName = personName;
  }

  /**
   * @return the personSurname
   */
  public String getPersonSurname() {
    return personSurname;
  }

  /**
   * @param personSurname the personSurname to set
   */
  public void setPersonSurname(String personSurname) {
    this.personSurname = personSurname;
  }

  /**
   * @return the personID
   */
  public String getPersonID() {
    return personID;
  }

  /**
   * @param personID the personID to set
   */
  public void setPersonID(String personID) {
    this.personID = personID;
  }

Customer

public class Customer extends Person implements Serializable {

  private String consultant;

  /**
   * @return the consultant
   */
  public String getConsultant() {
    return consultant;
  }

  /**
   * @param consultant the consultant to set
   */
  public void setConsultant(String consultant) {
    this.consultant = consultant;
  }

}
java  swing  serialization  file-io  text-files