FileOutputStream(File, append) 写入时不追加

FileOutputStream(File, append) does not append when writing

此代码是对象的一部分,每个对象都应将其位置写入 accountstorage.txt,但它只写入第一个对象位置,即使我将追加设置为 true

accountstorage = new File(currDir + "/Clients/accountstorage.txt");

        try {
            if(!accountstorage.exists()) {
        accountstorage.getParentFile().mkdirs();
        accountstorage.createNewFile();
            } else {
                return;
            }


            fos = new FileOutputStream(accountstorage, true);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));


        bw.write("@" + accountfile.getParentFile() + "\r\n");
        bw.flush();
        bw.close();

这可能是什么原因造成的?我自己似乎找不到问题。

因为你的条件块

 if(!accountstorage.exists()) {
    accountstorage.getParentFile().mkdirs();
    accountstorage.createNewFile();
} else {
    return; // DUE to this... Remove else block to fix.
}

当您的文件不存在时,它会创建它并写入位置,但下次当您的文件存在时,它会 returns 并且不写入任何内容。

希望对您有所帮助。