Java 关闭框架时保存对象文件

Java 关闭框架时保存对象文件,java,swing,serialization,io,Java,Swing,Serialization,Io,所以我要做的是让程序在用户单击退出按钮时保存文件,这样下次我运行它时,第一次输入的所有用户都仍然在那里。每次我重新运行程序时,文件都是空的,就好像第一次运行从未发生过一样。我需要做什么才能保存文件 这就是我现在所拥有的,但它只保存了第一次运行时的输入,并且在运行时从不添加新用户。我把东西放错地方了还是忘了什么 public class Admin extends JFrame implements ActionListener{ static ArrayList<User> us

所以我要做的是让程序在用户单击退出按钮时保存文件,这样下次我运行它时,第一次输入的所有用户都仍然在那里。每次我重新运行程序时,文件都是空的,就好像第一次运行从未发生过一样。我需要做什么才能保存文件

这就是我现在所拥有的,但它只保存了第一次运行时的输入,并且在运行时从不添加新用户。我把东西放错地方了还是忘了什么

public class Admin extends JFrame  implements ActionListener{

static ArrayList<User> users = new ArrayList<User>();
static FileOutputStream fos;
FileInputStream fis;
static ObjectOutputStream oos;
ObjectInputStream ois;
JTextField realname = new JTextField("", 20);
JTextField username = new JTextField("", 20);
JTextField password = new JTextField("", 20);
JTextField repassword = new JTextField("", 20);
JLabel rnlabel = new JLabel("Real Name");
JLabel unlabel = new JLabel("Username");
JLabel pwlabel = new JLabel("Password");
JLabel rpwlabel = new JLabel("Rewrite Password");
String name;
String uname;
StringBuilder S = new StringBuilder("Registered Users\n");



//--------------------------------------------------------------------------
public static void main(String[] args) throws ClassNotFoundException,FileNotFoundException,IOException, NoSuchAlgorithmException {
try{
    Admin admin = new Admin();
}
catch (ClassNotFoundException e){ 
    System.err.println(" A ClassNotFoundException error was found in main."); 
    e.printStackTrace();
    }       
//catch (NoSuchAlgorithmException e){ System.err.println("SHA-1 algorithm not available"); System.exit(0);}
catch (FileNotFoundException e){
    System.err.println(" A FileNotFoundException error was found in main.");
    e.printStackTrace();
    }
catch (IOException e){
    System.err.println(" A IOException error was found in main.");
    }
}
//--------------------------------------------------------------------------
// Data members for the controller. 
//--------------------------------------------------------------------------
JTabbedPane tp = new JTabbedPane();

JButton quitButton = new JButton("Quit");

JPanel newCard = new JPanel(new BorderLayout());
JButton submitButton = new JButton("Submit");
JPanel buttonPanel1 = new JPanel( new GridLayout(6, 1));
JTextArea data = new JTextArea();
JScrollPane userPane = new JScrollPane( data );

//--------------------------------------------------------------------------
public Admin() throws IOException, ClassNotFoundException, FileNotFoundException {
    setBounds(200, 200, 800, 200);
    initComponents();
    submitButton.addActionListener(this);
    quitButton.addActionListener(this);
    setVisible(true);


}

//--------------------------------------------------------------------------
public void actionPerformed( ActionEvent e) {

    try (ObjectInputStream is = new ObjectInputStream(new FileInputStream("userfile.txt"))) {
        users = (ArrayList<User>) is.readObject();
        updateList();
    } catch (IOException | ClassNotFoundException exp) {
        exp.printStackTrace();
    }

    Object source = e.getSource();
    if (source == submitButton){
        name = realname.getText();
        uname = username.getText();
        getPassword();
        for (int i = 0; i < users.size(); i++){
            System.out.println(users.get(i).toString());
        }
        updateList();
    }

    if (source == quitButton) {
        try (ObjectOutputStream os = new ObjectOutputStream(
                new FileOutputStream("userfile.txt"))) {
            os.writeObject(users);
        } catch (IOException exp) {
            exp.printStackTrace();
        }
        System.exit(0);
    }
}

//----------------------------------------------------------------------------  
protected void updateList() {
    for (User object : users) {
        S.append(object + "\n");
    }
    String text = S.toString();
    data.setText(text);
}


//------------------------------------------------------------------------------    
private void getPassword() {

    String pw = password.getText().toString();
    String pword_check = repassword.getText().toString();
    if (pw.equals(pword_check)){
        users.add(new User(name, uname, pw));
        realname.setText("");
        username.setText("");
        password.setText("");
        repassword.setText("");
    }
    else{
        JOptionPane.showMessageDialog( null, 
                "Passwords do not match. Re-enter both of them.");
        password.setText("");
        repassword.setText("");
    }
}

//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
void initComponents(){
    //-- The New User Screen -----------------------------------------------
    JPanel rname = new JPanel( new GridLayout(1, 3));
    rname.setLayout( new GridLayout(1, 2));
    rname.add(realname);
    rname.add(rnlabel);

    JPanel uname = new JPanel( new GridLayout(1, 3));
    uname.setLayout( new GridLayout(1, 2));
    uname.add(username);
    uname.add(unlabel);

    JPanel npword = new JPanel( new GridLayout(1, 3));
    npword.setLayout( new GridLayout(1, 2));
    npword.add(password);
    npword.add(pwlabel);

    JPanel nrpword = new JPanel( new GridLayout(1, 3));
    nrpword.setLayout( new GridLayout(1, 2));
    nrpword.add(repassword);
    nrpword.add(rpwlabel);

    buttonPanel1.add(rname, BorderLayout.CENTER);
    buttonPanel1.add(uname, BorderLayout.CENTER);
    buttonPanel1.add(npword, BorderLayout.CENTER);
    buttonPanel1.add(nrpword, BorderLayout.CENTER);
    buttonPanel1.add(submitButton, BorderLayout.CENTER);
    buttonPanel1.add(quitButton, BorderLayout.CENTER);
    buttonPanel1.setPreferredSize(new Dimension(150, 10));
    newCard.add(buttonPanel1);
    userPane.setPreferredSize(new Dimension(350, 50));
    newCard.add(userPane, "East");


    //-------------------------------------------------------------------------
    tp.addTab( "New User", newCard);
    super.setTitle("Login System");
    Container pane = getContentPane();
    pane.add(tp, BorderLayout.CENTER);  
}

}
公共类管理扩展JFrame实现ActionListener{
静态ArrayList用户=新建ArrayList();
静态文件输出流;
文件输入流fis;
静态对象输出流oos;
目的输入流ois;
JTextField realname=新的JTextField(“,20);
JTextField用户名=新的JTextField(“,20);
JTextField password=新的JTextField(“,20);
JTextField repassword=新的JTextField(“,20);
JLabel rnlabel=新JLabel(“实名”);
JLabel unlabel=新的JLabel(“用户名”);
JLabel pwlabel=新的JLabel(“密码”);
JLabel rpwlabel=新JLabel(“重写密码”);
字符串名;
字符串uname;
StringBuilder S=新StringBuilder(“注册用户”);
//--------------------------------------------------------------------------
公共静态void main(字符串[]args)抛出ClassNotFoundException、FileNotFoundException、IOException、NoSuchAlgorithmException{
试一试{
Admin=newadmin();
}
catch(classnotfound异常){
System.err.println(“在main中发现ClassNotFoundException错误”);
e、 printStackTrace();
}       
//catch(nosuchalgorithme异常){System.err.println(“SHA-1算法不可用”);System.exit(0);}
catch(filenotfounde异常){
System.err.println(“在main中发现FileNotFoundException错误”);
e、 printStackTrace();
}
捕获(IOE异常){
System.err.println(“在main中发现IOException错误”);
}
}
//--------------------------------------------------------------------------
//控制器的数据成员。
//--------------------------------------------------------------------------
JTabbedPane tp=新的JTabbedPane();
JButton quitButton=新JButton(“退出”);
JPanel newCard=newjpanel(newborderlayout());
JButton submitButton=新JButton(“提交”);
JPanel buttonPanel1=新的JPanel(新的网格布局(6,1));
JTextArea数据=新的JTextArea();
JScrollPane userPane=新的JScrollPane(数据);
//--------------------------------------------------------------------------
public Admin()抛出IOException、ClassNotFoundException、FileNotFoundException{
立根(200200800200);
初始化组件();
submitButton.addActionListener(此);
quitButton.addActionListener(此);
setVisible(真);
}
//--------------------------------------------------------------------------
已执行的公共无效操作(操作事件e){
try(ObjectInputStream=newobjectinputstream(newfileinputstream(“userfile.txt”)){
users=(ArrayList)为.readObject();
updateList();
}catch(IOException | ClassNotFoundException exp){
exp.printStackTrace();
}
对象源=e.getSource();
如果(源==提交按钮){
name=realname.getText();
uname=username.getText();
getPassword();
对于(int i=0;iif (source == quitButton) {
            try {
                fis = new FileInputStream("userfile.txt");
                ois = new ObjectInputStream(fis);
                ois.readObject();
                ois.close();
            } catch (IOException | ClassNotFoundException ev) {
                ev.printStackTrace();
            }
            System.exit(0);
        }
if (source == quitButton) {
    try {
        fis = new FileInputStream("userfile.txt");
        ois = new ObjectInputStream(fis);
        ois.readObject();
        ois.close();
    } catch (IOException | ClassNotFoundException ev) {
        ev.printStackTrace();
    }
    System.exit(0);
}
if (source == quitButton) {
    try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("userfile.txt"))) {
        os.writeObject(users);
    } catch (IOException exp) {
        exp.printStackTrace();
    }
    System.exit(0);
}
try (ObjectInputStream is = new ObjectInputStream(new FileInputStream("userfile.txt"))) {
    users = (ArrayList<User>) is.readObject();
} catch (IOException exp) {
    exp.printStackTrace();
}
protected void updateList() {
    for (User object : users) {
        S.append(object + "\n");
    }
    String text = S.toString();
    data.setText(text);
}
try (ObjectInputStream is = new ObjectInputStream(new FileInputStream("userfile.txt"))) {
    users = (ArrayList<User>) is.readObject();
    updateList();
} catch (IOException exp) {
    exp.printStackTrace();
}
public void actionPerformed(ActionEvent e) {

    Object source = e.getSource();
    if (source == submitButton) {
        name = realname.getText();
        uname = username.getText();
        getPassword();
        for (int i = 0; i < users.size(); i++) {
            System.out.println(users.get(i).toString());
        }
        updateList();
    } else if (source == quitButton) {
        try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("userfile.txt"))) {
            os.writeObject(users);
        } catch (IOException exp) {
            exp.printStackTrace();
        }
        System.exit(0);
    }
}