gpt4 book ai didi

android - 将文本附加到文件末尾

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:56:54 28 4
gpt4 key购买 nike

每次调用时,我都使用下面的代码段将文本写入文件的末尾。但是,它正在删除旧数据,然后将新数据写入文件的开头。如何修复以下代码,使其始终在文件末尾追加新数据?

public boolean writeToFile(String directory, String filename, String data ){
File out;
OutputStreamWriter outStreamWriter = null;
FileOutputStream outStream = null;

out = new File(new File(directory), filename);

if ( out.exists() == false ){
out.createNewFile();
}

outStream = new FileOutputStream(out) ;
outStreamWriter = new OutputStreamWriter(outStream);

outStreamWriter.append(data);
outStreamWriter.flush();
}

最佳答案

尝试在 FileOutputStream 中将 append boolean 值设置为 true:

outStream = new FileOutputStream(out, true);
outStreamWriter = new OutputStreamWriter(outStream);

关于android - 将文本附加到文件末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14872429/

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