3

i've edited the code, can you please tell me how i can write each line at the end of the file? Here it's been written on the same line. Can you help please?

private void okMouseClicked(java.awt.event.MouseEvent evt) {                                
    String pass = name.getText();
    if (pass.equals("")){
        error.setText("Name of Entity should not be left blank");
        //JOptionPane.showMessageDialog(null,"Name of Entity should not be left blank");
    }
    else{
        try{

            Formatter outfile = new Formatter(new FileOutputStream("Convert.sql", true));
            outfile.format("Create Database IF NOT EXISTS " + pass + ";");
            outfile.close();

        }

        catch(FileNotFoundException fnfe){
            System.out.println("Not found");

        }
        this.dispose();
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Lana
  • 171
  • 1
  • 7
  • 18

1 Answers1

0

You can pass to the constructor of Formatter a FileOutputStream created in append mode:

Formatter outfile = new Formatter(new FileOutputStream("Convert.sql", true));
outfile.format("Create Database IF NOT EXISTS " + pass + ";\n");  // \n to add new line

See http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#Formatter(java.io.OutputStream).

M A
  • 71,713
  • 13
  • 134
  • 174
  • Thanks it worked @manouti. But How can i add each statement at the end of the file and not on the same line? – Lana Feb 09 '15 at 19:13
  • @LanaM Just add a newline (`\n`) character when writing: `outfile.format("Create Database IF NOT EXISTS " + pass + ";\n");` – M A Feb 09 '15 at 19:25