如何防止文件覆盖自己?

时间:2015-02-09 18:11:51

标签: java java-io

我编辑了代码,你能否告诉我如何在文件末尾写下每一行?这里写的是同一条线。你能帮忙吗?

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();
    }
}

1 个答案:

答案 0 :(得分:0)

您可以传递给在附加模式中创建的FormatterFileOutputStream的构造函数:

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

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