简体   繁体   English

如何以编程方式在Android中读取/写入数据字典

[英]how to read/write data dictionary in android programmatically

I want to read/write data dictionary in file which is in android internal/external memory. 我想在Android内部/外部存储器中的文件中读取/写入数据字典。 In WP7, they have used IsolatedStorage for storing the dictionary directly. 在WP7中,他们使用了IsolatedStorage直接存储字典。 In IOS, they can write NSDictionary directly to the file. 在IOS中,他们可以将NSDictionary直接写入文件。 Please anyone tell me the way to write DataDictionary into file. 请任何人告诉我将DataDictionary写入文件的方法。

Note: I have the keys and values in the Map variable. 注意:我在Map变量中有键和值。 how to store this Map directly to file 如何将此地图直接存储到文件

I would suggest putting your words into a database for the following reasons 我建议出于以下原因将您的话放到数据库中

DB lookups on android with SQLite are "fast enough" (~1ms) for even 使用SQLite在Android上进行数据库查找的速度“足够快”(〜1ms),甚至
the most impatient of users 最不耐烦的用户

Reading large files into memory is a dangerous practice in memory-limited environments such as android. 在内存受限的环境(例如android)中,将大文件读入内存是一种危险的做法。

Trying to read entries out of a file "in place" rather than "in 尝试从文件“就地”而不是“在”中读取条目
memory" is effectively trying to solve all the problems that SQLite 内存”正在有效地尝试解决SQLite的所有问题
already solves for you. 已经为您解决。

Embed a database in the .apk of a distributed application [Android] 将数据库嵌入分布式应用程序的.apk中[Android]

You can find more detailed examples by searching object serialization 您可以通过搜索对象序列化找到更详细的示例

[EDIT 1] [编辑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);

[EDIT 2] [编辑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) {

    }

[EDIT 3] [编辑3]

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

     // check for value

}

I'm unsure if using SharedPreferences ( link ) is something that is suitable for your usecase. 我不确定使用SharedPreferenceslink )是否适合您的用例。

You store via a key-value pair, and can have multiple SharedPreferences per application. 您通过键值对进行存储,并且每个应用程序可以具有多个SharedPreferences While both are stored as String objects, the value can be automatically cast to other primitives using built in methods. 当两者都存储为String对象时,可以使用内置方法将值自动转换为其他基元。

Mark Murphy has written a library, cwac-loaderex ( link ), to facilitate access of SharedPreferences via the use of the Loader pattern ( link ), which offsets some of the work you need to do to keep IO off the main thread. Mark Murphy已经编写了一个库cwac-loaderexlink ),以通过使用Loader模式( link )来促进对SharedPreferences访问,这抵消了使IO脱离主线程所需的一些工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM