按下jButton时如何将数据追加到文件中

时间:2018-07-04 00:56:20

标签: java

我一直在创建一个GUI应用程序,为学生注册数据,数据填充在提供的JTextfields中。按下提交按钮后,我对其进行了编程,以便以我喜欢的某种方式(排列)将数据存储到文件中。对于第一个提交,该程序运行良好,并将数据存储到文件中, ,但是在其他连续enter code here个提交中(即,当学生重新填充时),该程序将覆盖或不写入完全放在文件中 。下面是到目前为止的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;

public class StudentAssignment extends JFrame {
    private JLabel lblFname, lblbSname, lblRegNo, lblPos;
    private JButton btnsubmit;
    private JTextField txtFname, txtSname, txtRegNo, txtPos;
    private JPanel panel1, panel12, panel3, panel4,panel5;
    private int i = 1;
    private File myFile;
    private FileWriter fw;
    private PrintStream ps;


    public StudentAssignment() throws Exception{
        componentInitialization();
        setVisible(true);

        myFile =new File("Mustard.txt");

        fw = new FileWriter(myFile,true);
        ps = new PrintStream(myFile);

        actionListeners();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void componentInitialization(){
        setBounds(200, 200, 300, 300);
        setTitle("Mustard's Frame");
        setLayout(new GridLayout(5,1));

        lblFname = new JLabel("First Name");
        txtFname = new JTextField(10);
        panel1 = new JPanel();
        panel1.add(lblFname); panel1.add(txtFname);

        lblbSname = new JLabel("Surname");
        txtSname = new JTextField(10);
        panel12 = new JPanel();
        panel12.add(lblbSname); panel12.add(txtSname);

        lblRegNo = new JLabel("Registration Number");
        txtRegNo = new JTextField(10);
        panel3 = new JPanel();
        panel3.add(lblRegNo); panel3.add(txtRegNo);

        lblPos = new JLabel("program of study");
        txtPos = new JTextField(10);
        panel4 = new JPanel();
        panel4.add(lblPos); panel4.add(txtPos);

        btnsubmit = new JButton("SUBMIT");
        panel5 = new JPanel();
        panel5.add(btnsubmit);

        add(panel1); add(panel12);
        add(panel3); add(panel4);
        add(panel5);

    }

    public void actionListeners(){

        btnsubmit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)  {

                try {
                    fileStoring();
                }catch (Exception exc){

                }

                clearFields();
            }
        });
    }

    public void fileStoring() throws Exception{

        System.setOut(ps);

        System.out.printf("%-10s %-15s %-15s %-15s %-15s\n", "Sno.", "First Name", "Surname", "Reg.No", "Program of Study");
        System.out.printf("%-10d %-15s %-15s %-15s %-15s\n",i,txtFname.getText(), txtSname.getText(), txtRegNo.getText(), txtPos.getText()  );
        i++;
        fw.close();
        ps.close();
    }

    public void clearFields(){
        txtFname.setText("");
        txtSname.setText("");
        txtRegNo.setText("");
        txtPos.setText("");

        txtFname.requestFocus();
    }

    public static  void  main(String[] args) throws Exception{
        StudentAssignment obj = new StudentAssignment();
    }

}

2 个答案:

答案 0 :(得分:0)

像这样更改您的方法,

from docx.enum.text import WD_LINE_SPACING
from docx import Document

document = Document('demo.docx')
smi_line = document.add_paragraph('test line spacing')
smi_line.line_spacing_rule = WD_LINE_SPACING.SINGLE
document.save('demo.docx')

为什么无法获得理想的结果是在构造函数内部初始化流,然后在第一次写入后关闭流。

public void fileStoring() throws Exception {
        try (FileWriter fileWriter = new FileWriter("C:\\data\\Mustard.txt", true);
                PrintWriter printWriter = new PrintWriter(fileWriter)) {
            if (i == 1) {
                printWriter.printf("%-10s %-15s %-15s %-15s %-15s\n", "Sno.", "First Name", "Surname", "Reg.No",
                        "Program of Study");
            }
            printWriter.printf("%-10d %-15s %-15s %-15s %-15s\n", i, txtFname.getText(), txtSname.getText(),
                    txtRegNo.getText(), txtPos.getText()).toString();
        } catch (IOException e) {
            // handle the exception here.
        }

        i++;
    }

然后,后续写入将失败。为了解决这个问题,您每次写入文件时都需要创建一个新的流(即在此方法本身内部,而不是在构造函数内部),然后将其关闭。还要注意,您只需要在开始时编写标头,而您的实现也不能处理它。

答案 1 :(得分:0)

工作成功,非常感谢您的贡献。

在您提出建议之后,下面是经过编辑的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class StudentAssignment extends JFrame {

private JLabel lblFname, lblbSname, lblRegNo, lblPos;
private JButton btnsubmit;
private JTextField txtFname, txtSname, txtRegNo, txtPos;
private JPanel panel1, panel12, panel3, panel4,panel5;
private int i = 1;

public StudentAssignment() throws Exception{
    componentInitialization();
    setVisible(true);
    actionListeners();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void componentInitialization(){
    setBounds(200, 200, 300, 300);
    setTitle("Mustard's Frame");
    setLayout(new GridLayout(5,1));

    lblFname = new JLabel("First Name");
    txtFname = new JTextField(10);
    panel1 = new JPanel();
    panel1.add(lblFname); panel1.add(txtFname);

    lblbSname = new JLabel("Surname");
    txtSname = new JTextField(10);
    panel12 = new JPanel();
    panel12.add(lblbSname); panel12.add(txtSname);

    lblRegNo = new JLabel("Registration Number");
    txtRegNo = new JTextField(10);
    panel3 = new JPanel();
    panel3.add(lblRegNo); panel3.add(txtRegNo);

    lblPos = new JLabel("program of study");
    txtPos = new JTextField(10);
    panel4 = new JPanel();
    panel4.add(lblPos); panel4.add(txtPos);

    btnsubmit = new JButton("SUBMIT");
    panel5 = new JPanel();
    panel5.add(btnsubmit);

    add(panel1); add(panel12);
    add(panel3); add(panel4);
    add(panel5);

}

public void actionListeners(){

    btnsubmit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)  {

            try {
                fileStoring();
            }catch (Exception exc){

            }

            clearFields();
        }
    });
}

public void fileStoring() throws Exception {
    try (FileWriter fileWriter = new FileWriter("Mustard.txt", true);
         PrintWriter printWriter = new PrintWriter(fileWriter)) {
        if (i == 1) {
            printWriter.printf("%-10s %-15s %-15s %-15s %-15s\n", "Sno.", "First Name", "Surname", "Reg.No",
                    "Program of Study");
        }
        printWriter.printf("%-10d %-15s %-15s %-15s %-15s\n", i, txtFname.getText(), txtSname.getText(),
                txtRegNo.getText(), txtPos.getText()).toString();
    } catch (IOException e) {

    }

    i++;
}

public void clearFields(){
    txtFname.setText("");
    txtSname.setText("");
    txtRegNo.setText("");
    txtPos.setText("");

    txtFname.requestFocus();
}

public static  void  main(String[] args) throws Exception{
    StudentAssignment obj = new StudentAssignment();
}

}