使用 Properties 类加载地图 [英] Loading a map using Properties class

查看:30
本文介绍了使用 Properties 类加载地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 75000 个条目的地图,每个条目值的大小平均为 10kb.

I have a map with 75000 entries and each entry value will be of size 10kb on average.

我使用 Properties 类将此地图加载到内存中.但是由于地图的大小,当主机上的RAM较小时,我会出现OutOfMemoryException.

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.

我的一个选择是将条目分批(如 10,000 个)读入内存,而不是加载完整的地图.处理完最初的 10k 后读取下一个 10k.

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.

有没有办法使用 Properties 类来实现这一点.

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

  1. 将条目分成多个文件

  1. Divide entries into multiple files

使用 首选项

示例代码:

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();
        }
    }
}

示例 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆