2010-10-05 49 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)會比創建自己的系統更簡單。