Donc, ce que j'essaie de faire, c'est que le programme enregistre le fichier lorsque l'utilisateur clique sur le bouton Quitter, de sorte que la prochaine fois que je l'exécute, tous les utilisateurs sont toujours là. Chaque fois que je réexécute le programme, le fichier est vide et c'est comme si le premier passage n'avait jamais eu lieu. Que dois-je faire pour enregistrer le fichier?Enregistrer un fichier objet lorsque le cadre est fermé
Donc, c'est ce que j'ai maintenant, mais il enregistre seulement l'entrée de la première exécution et n'ajoute jamais de nouveaux utilisateurs quand je l'exécute. Ai-je mis quelque chose au mauvais endroit ou oublié quelque chose?
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);
}
}
Vous lisez l'objet lorsque vous traitez quitButton? – MadProgrammer