将选定的单选按钮值和文本字段值保存到文件中

时间:2017-04-05 14:10:52

标签: java

我是否知道如何通过GUI Form将选定的单选按钮值和文本字段值保存到文件中?

表单看起来像this

我的代码:

 private void SaveButActionPerformed(java.awt.event.ActionEvent evt) {                                        
    try{
    File f=new File("Customer detail.txt");
    String id = this.CusIDTxt.getText();
    String name=NameTxt.getText();
    String ic=ICNoTxt.getText();
    String ph=PhNoTxt.getText();
    String email=EmailAddTxt.getText();
    PrintWriter writer=new PrintWriter(new BufferedWriter(new FileWriter(f,true)));
    writer.println(id);
    writer.println(name);
    writer.println(ic);
    writer.println(ph);
    writer.println(email);
    writer.close();
    JOptionPane1.showMessageDialog(this, "Register customer was successful");
    m.setVisible(true);
    this.setVisible(false);
    }
    catch(IOException ioe)
    {
        JOptionPane1.showMessageDialog(this,"All fields must be completed in order to register");
    }
}                                    

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您调用了文件编写器,但您没有调用方法来编写它。例如,您可以尝试writer.write(name)。它会将输入写入文件。您可以查看此API的文档https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html