尝试使用 Java 写入文件时如何下线

How to go down lines when trying to write to a file with Java

使用下面的代码,我正在尝试为一个插件创建一个日志,但是,每次它创建一个条目时,我都希望它位于另一个条目的下方。虽然,当我测试它时,它会写入第二个条目,但第一个条目会被删除。

if (commandLabel.equalsIgnoreCase("logmeup")) {
    entry++;
    entry3 = entry * 3;
    FileWriter fw= null;
    File file =null;

    try {
        file = new File("playerLog.txt");
        if(!file.exists()) {
            file.createNewFile();
        }
        fw = new FileWriter(file);
        if (entry == 1) {
            fw.write("Name: " + player.getDisplayName() + "\nIP: " + player.getAddress() + "\nLocation: " + player.getLocation() + "\n");
            fw.close();
        }
        else {
            while (entryloop < entry3) {
                entryloop++;
                fw.write(System.getProperty( "line.separator" ));
            }
            entryloop = 0;
            fw.write("Name: " + player.getDisplayName() + "\nIP: " + player.getAddress() + "\nLocation: " + player.getLocation() + "\n");
            fw.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

被标记为欺骗但我会再次回答: 将 FileWriter 的第二个参数传递为 true 以使其进入追加模式。

fw = new FileWriter(file,true);