代码之家  ›  专栏  ›  技术社区  ›  Emil

Java update数据结构更改为序列化文件

  •  1
  • Emil  · 技术社区  · 16 年前

    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?

    2 回复  |  直到 16 年前
        1
  •  1
  •   Jigar Joshi    16 年前

    使用 DB 还是简单 File IO

        2
  •  1
  •   Colin Hebert    16 年前

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

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

    推荐文章