gpt4 book ai didi

Java输入流追加而不删除

转载 作者:行者123 更新时间:2023-12-01 12:26:38 25 4
gpt4 key购买 nike

如何在 FileInputStream(...) 之后将数据追加到文件中而不使其干净?

这就是我从我的 HashMap <String, String> storage 中 append 的方式:

DataOutputStream stream = new DataOutputStream(new FileOutputStream("myfile.dat"));
for (Map.Entry<String, String> entry : storage.entrySet()) {
byte[] bytesKey = entry.getKey().getBytes("UTF-8"); //I need to write using UTF8
stream.writeInt(bytesKey.length);
stream.write(bytesKey);
byte[] bytesVal = entry.getValue().getBytes("UTF-8");
stream.write(bytesVal);
}
stream.close();

所以,问题是当我再次打开它时,它会清除文件中以前的所有数据。如何避免这种情况?

最佳答案

向 FileOutputStream 添加 true 参数,以便其追加

DataOutputStream stream = new DataOutputStream(new FileOutputStream("myfile.dat", true));

关于Java输入流追加而不删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26282470/

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