即使autoFlush设置为true,Java也会覆盖文件而不是附加文件

时间:2019-06-18 11:57:27

标签: java filewriter

我编写了一个简单的程序来写入文件,但是每次我调用构造函数而不是追加到文件时,代码都会覆盖该文件。

我尝试将autoFlush设置为true,但是没有用。

public class Credentials {
    private String username;
    private String password;
    private static  Map<String, String> credentials = new HashMap<>();


    public Credentials(String username, String password) {
        this.username = username;
        this.password = password;

        try {
            FileWriter fileWriter = new FileWriter("credentials.txt");
            PrintWriter printWriter = new PrintWriter(fileWriter, true);
            printWriter.write(username + " " + password + " \n");
            printWriter.close();
        }
        catch(IOException ex) {
            System.out.println("Error");
        }
    }
}

0 个答案:

没有答案