【问题标题】:how to read/write data dictionary in android programmatically如何以编程方式在android中读/写数据字典
【发布时间】:2013-01-08 10:19:13
【问题描述】:

我想在 android 内部/外部存储器中的文件中读取/写入数据字典。在 WP7 中,他们使用了 IsolatedStorage 直接存储字典。在 IOS 中,他们可以直接将 NSDictionary 写入文件。请任何人告诉我将 DataDictionary 写入文件的方法。

注意:我在 Map 变量中有键和值。 如何将此地图直接存储到文件中

【问题讨论】:

    标签: android storage read-write data-dictionary


    【解决方案1】:

    出于以下原因,我建议将您的话放入数据库

    使用 SQLite 在 android 上查找数据库“足够快”(~1ms)甚至
    最不耐烦的用户

    将大文件读入内存是一种危险的做法 内存有限的环境,例如 android。

    试图从“就地”而不是“在”文件中读取条目
    内存”正在有效地尝试解决 SQLite 的所有问题
    已经为你解决了。

    Embed a database in the .apk of a distributed application [Android]

    您可以通过搜索对象序列化

    找到更详细的示例

    [编辑 1]

    Map map = new HashMap();
    map.put("1",new Integer(1));
    map.put("2",new Integer(2));
    map.put("3",new Integer(3));
    FileOutputStream fos = new FileOutputStream("map.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(map);
    oos.close();
    
    FileInputStream fis = new FileInputStream("map.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Map anotherMap = (Map) ois.readObject();
    ois.close();
    
    System.out.println(anotherMap);
    

    [编辑 2]

    try {
    
            File file = new File(getFilesDir() + "/map.ser");
    
            Map map = new HashMap();
            map.put("1", new Integer(1));
            map.put("2", new Integer(2));
            map.put("3", new Integer(3));
            Map anotherMap = null;
    
            if (!file.exists()) {
                FileOutputStream fos = new FileOutputStream(file);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(map);
                oos.close();
    
                System.out.println("added");                
            } else {
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                anotherMap = (Map) ois.readObject();
                ois.close();
    
                System.out.println(anotherMap);
            }
    
    
    
        } catch (Exception e) {
    
        }
    

    [编辑 3]

    Iterator myVeryOwnIterator = meMap.keySet().iterator();
    while(myVeryOwnIterator.hasNext()) {
        String key=(String)myVeryOwnIterator.next();
        String value=(String)meMap.get(key);
    
         // check for value
    
    }
    

    【讨论】:

    • 但是如果你把你的键值存储在文件中,成本太高了。我的意思是当你尝试搜索一个词时,你怎么能做到这一点?
    • 在存储时我已经将键和值附加了一些值,所以我可以拆分它
    • 我不想在这里使用 SQLite 存储。还有其他选择吗?
    • 谢谢。 “map.ser”将存储在哪里?什么是“.ser”?
    • .ser 是无意义的,只是序列化的最短单词。请看编辑2
    【解决方案2】:

    我不确定使用 SharedPreferences (link) 是否适合您的用例。

    您通过键值对存储,每个应用程序可以有多个SharedPreferences。虽然两者都存储为 String 对象,但可以使用内置方法将值自动转换为其他原语。

    Mark Murphy 编写了一个库 cwac-loaderex (link),以方便通过使用 Loader 模式 (link) 访问 SharedPreferences,它可以抵消一些您需要的工作做以保持 IO 离开主线程。

    【讨论】:

      猜你喜欢
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 2017-07-17
      • 2013-03-21
      • 2021-11-25
      相关资源
      最近更新 更多