1

I have a user password application that writes onto a file - you enter the information in the textfields, click a button and it should write to the next line of the file created.

There are two problems:

  1. The file only writes one line of data
  2. It writes only what you entered last. I have tried validate() and repaint() but no difference.
public class Main {

public JFrame frame;
public JTextField textField;
public JTextField textField_1;
public JTextField textField_2;
public JTextField textField_3;

public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Main window = new Main();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });


}

public Main() {
    initialize();
}

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 588, 156);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnNewButton = new JButton("Add Record");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            openFile();
            printFormat();
            addRecords();
            closeFile();

        }
    });
    btnNewButton.setBounds(237, 83, 91, 23);
    frame.getContentPane().add(btnNewButton);

    textField = new JTextField();
    textField.setColumns(10);
    textField.setBounds(10, 44, 128, 20);
    frame.getContentPane().add(textField);

    textField_1 = new JTextField();
    textField_1.setColumns(10);
    textField_1.setBounds(148, 44, 128, 20);
    frame.getContentPane().add(textField_1);

    textField_2 = new JTextField();
    textField_2.setFont(UIManager.getFont("PasswordField.font"));
    textField_2.setColumns(10);
    textField_2.setBounds(286, 44, 128, 20);
    frame.getContentPane().add(textField_2);

    textField_3 = new JTextField();
    textField_3.setColumns(10);
    textField_3.setBounds(424, 44, 128, 20);
    frame.getContentPane().add(textField_3);

    JLabel lblNewLabel = new JLabel("Username");
    lblNewLabel.setBounds(10, 19, 128, 14);
    frame.getContentPane().add(lblNewLabel);

    JLabel lblPassword = new JLabel("Email");
    lblPassword.setBounds(148, 19, 128, 14);
    frame.getContentPane().add(lblPassword);

    JLabel lblMojangPassword = new JLabel("Mojang Password");
    lblMojangPassword.setBounds(286, 19, 128, 14);
    frame.getContentPane().add(lblMojangPassword);

    JLabel lblEmailPassword = new JLabel("Email Password");
    lblEmailPassword.setBounds(424, 19, 128, 14);
    frame.getContentPane().add(lblEmailPassword);

}


private Formatter x;

public void openFile(){
    try{
        x = new Formatter("okey.fil");

    }
    catch(Exception e){
        System.out.println("error get it right");
    }
}
public void printFormat(){
    x.format("%-25s %-25s %-25s %-25s \n", "Username", "Email", "Mojang Password", "Email Password");
}
public String getUsername(){
    String username = textField.getText();
    return username;
}
public void addRecords(){
        x.format("%-25s %-25s %-25s %-25s \n", getUsername(),textField_1.getText(),textField_2.getText(),textField_3.getText());
        x.format("\n"); 
        resetFields();
    }


public void resetFields(){
    textField.setText("");
    textField_1.setText("");
    textField_2.setText("");
    textField_3.setText("");
    textField.validate();
    textField_1.validate();
    textField_2.validate();
    textField_3.validate();
}
public void closeFile(){
    x.close();
}
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

1

The issue is the following: you open your file every time when action for "btnNewButton" is performed - the file is opened by java.lang.Formatter in "w" mode, which means it's truncated and only new line is written at the beginning. You actually write every line to the file, but only the last one is left.

Solution 1: you have to open Formatter (or FileOutputStream, or anything similar) only once - and append lines there. You can call flush() instead of close() to actually write the data to file.

Solution 2: you can still reopen file every time on action if you need it - but you have to open it an "append" mode - How to append text to an existing file in Java

For example, you can use this constructor (second parameter must be true) http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html#FileOutputStream%28java.io.File,%20boolean%29

Community
  • 1
  • 1
pkozlov
  • 746
  • 5
  • 17
0

You can use

x = new Formatter(new FileOutputStream("okey.fil",true));
Madhan
  • 5,750
  • 4
  • 28
  • 61
  • Could you please [edit] your answer to give an explanation of why this code answers the question? Code-only answers are [discouraged](http://meta.stackexchange.com/questions/148272), because they don't teach the solution. – DavidPostill Jun 21 '15 at 11:48