2010-10-05 74 views
1

我有一个包含大量条目的哈希映射,它是序列化的。如果我在哈希映射中做了一个小改动,它是否需要我完全覆盖旧文件或者是否存在替代方案?Java更新数据结构更改为序列化文件

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的?

回答

1

使用DB或简单File IO保持以前写入的位置。

1

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

也许它太过分了,但是NoSQL db(例如cassandra)会比创建自己的系统更简单。