gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:37:37 27 4
gpt4 key购买 nike

这段代码是一个对象的一部分,每个对象都应该将其位置写入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();

什么可能导致这种情况?我自己似乎找不到问题。

最佳答案

因为你的条件 block

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

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

希望这有帮助。

关于java - FileOutputStream(File,append)写入时不追加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38726527/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com