views:

32

answers:

2

I have a hashmap with large number of entry's which is serialized.If i make a small change in hashmap is it required that I overwrite the old file completely or is there an alternative ?

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
}
}

Since the change is only for the string "hello" and for no other element is it possible to update the serialized file only for the string "hello" instead of rewriting the whole hashmap once again ?

+1  A: 

AFAIK, you can't do incremental saves with the simple java serialization.
You should instead use another system to store your data (such as a database).

Maybe it's overkill but a NoSQL db (cassandra for instance) would be simpler than trying to create your own system.

Colin Hebert
+1  A: 

Use DB or simple File IO maintain upto where you have written previously .

org.life.java