使用Properties类加载地图 [英] Loading a map using Properties class
问题描述
我有一个包含75000个条目的地图,每个条目的值将平均大小为10kb。
我使用Properties类将该地图加载到内存中。但由于地图的大小,当主机上的RAM很小时,我会发生OutOfMemoryException。
我拥有的一个选项是批量读取条目(如10,000)加载到内存中,而不是加载完整的地图。
在处理初始10k后读取下一个10k。
是否有任何方法可以使用Properties类完成此操作。
另外,有没有更好的方法来以这种方式加载map条目?
感谢和问候,
Sujith
请勿使用属性,这是遗留的
-
条目到多个文件中
-
按顺序读取每个文件,使用首选项
示例代码:
package com.mypack.test;
import java.io. *;
import java.util。*;
import java.util.prefs.Preferences;
public class PreferencesExample {
public static void main(String args [])throws FileNotFoundException {
Preferences ps = Preferences.userNodeForPackage(PreferencesExample.class);
//加载文件对象
文件fileObj = new File(d:\\data.xml);
尝试{
FileInputStream fis = new FileInputStream(fileObj);
ps.importPreferences(fis);
System.out.println(Prefereces:+ ps);
System.out.println(Get property1:+ ps.getInt(property1,10));
} catch(Exception err){
err.printStackTrace();
示例xml:
<?xml version =1.0encoding =UTF-8?>
<!DOCTYPE首选项SYSTEM'http://java.sun.com/dtd/preferences.dtd'>
< preferences EXTERNAL_XML_VERSION =1.0>
< root type =user>
< map />
< node name =com>
< map />
< node name =mypack>
< map />
< node name =test>
< map>
< entry key =property1value =80/>
< entry key =property2value =Red/>
< / map>
< / node>
< / node>
< / node>
< / root>
< / preferences>
I have a map with 75000 entries and each entry value will be of size 10kb on average.
I load this map into memory using Properties class . But due to the size of the map , I get OutOfMemoryException when the RAM on the host is small.
One option that i have is to read the entries in batches (like 10,000) into memory instead of loading the complete map. Read the next 10k after processing the initial 10k.
Is there any way to accomplish this using Properties class.
Also, is there any better approach of loading the map entries in this manner?
Thanks and Regards,
Sujith
Don't use Properties, which is legacy
Divide entries into multiple files
Read each file in sequence, load and process using Preferences
Example code:
package com.mypack.test;
import java.io.*;
import java.util.*;
import java.util.prefs.Preferences;
public class PreferencesExample {
public static void main(String args[]) throws FileNotFoundException {
Preferences ps = Preferences.userNodeForPackage(PreferencesExample.class);
// Load file object
File fileObj = new File("d:\\data.xml");
try {
FileInputStream fis = new FileInputStream(fileObj);
ps.importPreferences(fis);
System.out.println("Prefereces:"+ps);
System.out.println("Get property1:"+ps.getInt("property1",10));
} catch (Exception err) {
err.printStackTrace();
}
}
}
Sample xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE preferences SYSTEM 'http://java.sun.com/dtd/preferences.dtd'>
<preferences EXTERNAL_XML_VERSION="1.0">
<root type="user">
<map />
<node name="com">
<map />
<node name="mypack">
<map />
<node name="test">
<map>
<entry key="property1" value="80" />
<entry key="property2" value="Red" />
</map>
</node>
</node>
</node>
</root>
</preferences>
这篇关于使用Properties类加载地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!