将数据保存到.txt文件中

时间:2018-12-08 23:01:08

标签: java

有人可以帮我吗?我想在按btnSubmit时将所有数据(如文本字段,复选框,radioButtton,滚动列表)保存在.txt文件中。

btnSubmit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if(textField.getText().isEmpty()||(textField_1.getText().isEmpty())||(textField_2.getText().isEmpty())||(textArea_1.getText().isEmpty())||((radioButton_1.isSelected())&&(radioButton.isSelected()))||(comboBox.getSelectedItem().equals("Select")))
                JOptionPane.showMessageDialog(null, "Data Missing");
            else 

             JOptionPane.showMessageDialog(null, "Data Submitted");



        }
    });

1 个答案:

答案 0 :(得分:1)

BufferedWriter writer = new BufferedWriter(new FileWriter(your_file, append)); 
writer.write(your_string);
writer.close();

对于btnSubmit:

btnSubmit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        if(textField.getText().isEmpty()||(textField_1.getText().isEmpty())||(textField_2.getText().isEmpty())||(textArea_1.getText().isEmpty())||((radioButton_1.isSelected())&&(radioButton.isSelected()))||(comboBox.getSelectedItem().equals("Select")))
            JOptionPane.showMessageDialog(null, "Data Missing");
        else {
         JOptionPane.showMessageDialog(null, "Data Submitted");
         BufferedWriter writer = new BufferedWriter(new 
         FileWriter(new File("file.txt"), append)); 
         writer.write(textField.getText());
         writer.close();
        }
});

但是Google搜索会提供比我更好的答案。