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

我一直在创建一个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);


       

月关宝盒
浏览 136回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java