gpt4 book ai didi

Java更新序列化文件的数据结构更改

转载 作者:行者123 更新时间:2023-11-30 05:09:44 25 4
gpt4 key购买 nike

我有一个包含大量序列化条目的 HashMap 。如果我对 HashMap 进行小更改,是否需要完全覆盖旧文件,或者是否有替代方案?

public class HashMapSerial {
public static void main(String[] args) {
HashMap<String,Integer> hash=new HashMap<String, Integer>(100000);
hash.put("hello",1 );
hash.put("world", 2);
//+ (100000 -2) entry's


ObjectOutputStream s=new ObjectOutputStream(new FileOutputStream(new File("hash.out")));
s.writeObject(hash); // write the hash map to file


hash.put("hello",10);
s.writeObject(hash); //rewrite the whole hashmap again
}
}

由于更改仅针对字符串“hello”,而没有其他元素,是否可以仅针对字符串“hello”更新序列化文件,而不是再次重写整个 HashMap ?

最佳答案

使用DB或简单的File IO维护到您之前编写的位置。

关于Java更新序列化文件的数据结构更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3861528/

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