java 使用文件和格式化程序时 txt 文件被覆盖

java txt file is over writing when use file and formatter

所以我已经尝试了所有方法,但文件一直在写第一行,我将这些方法传递给另一个 class 因此方法 add_records 中的三个参数通过扫描器传递。

这是 creating/opening 方法:

    public class Io_Files{


    private Formatter output;

public void open_file_normal(String filename) throws IOException
{
    {
        try
        {
            FileWriter fileWriter = new FileWriter(filename, true);
            output = new Formatter(filename);
        }
        catch(FileNotFoundException fileNotFoundException)
        {
            System.err.println("Error");
        }
    }
}

这里是添加记录方法的代码:

    public void add_records(String a, String b, int c) throws       FileNotFoundException{


output.format ("[%s, %s, %d]%n", a, b, c);



} 

如果您能够提供一些意见,说明为什么以及在哪里放置代码,那么我也可以学习。

干杯

尝试使用

output = new Formatter(fileWriter);

而不是

output = new Formatter(filename);