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

Java更新对序列化文件的数据结构更改,java,serialization,Java,Serialization,我有一个hashmap,其中包含大量序列化的条目。如果我在hashmap中做了一个小更改,是要求我完全覆盖旧文件还是有其他替代方法 public class HashMapSerial { public static void main(String[] args) { HashMap<String,Integer> hash=new HashMap<String, Integer>(100000); hash.put("hello",1 ); h

我有一个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
}
}
公共类HashMapSerial{
公共静态void main(字符串[]args){
HashMap hash=新的HashMap(100000);
hash.put(“hello”,1);
hash.put(“世界”,2);
//+(100000-2)输入
ObjectOutputStream s=新的ObjectOutputStream(新文件(“hash.out”));
s、 writeObject(哈希);//将哈希映射写入文件
hash.put(“你好”,10);
s、 writeObject(hash);//再次重写整个hashmap
}
}

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

AFAIK,您无法使用简单的java序列化进行增量保存。
您应该使用另一个系统来存储数据(例如数据库)


也许这有点过分,但NoSQL db(例如cassandra)比创建自己的系统要简单。

使用
db
或简单
文件IO
维护到您之前编写的位置