读取文件并获取key = value而不使用java.util.Properties [英] Read file and get key=value without using java.util.Properties

查看:547
本文介绍了读取文件并获取key = value而不使用java.util.Properties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个RMI游戏,客户端会加载一个文件,其中包含一些要用于多个不同对象的键和值。这是一个保存游戏文件,但我不能使用java.util.Properties(这是在规范)。我必须阅读整个文件,忽略注释行和在某些类中不相关的键。这些属性是唯一的,但可以按任意顺序排序。我的文件当前文件如下所示:

 #Bio 
playerOrigin = Newlands
playerClass = Warlock
#Armor
playerHelmet =空$ b $ playerUpperArmor = armor900
playerBottomArmor = armor457
playerBoots = boot109

这些属性将根据玩家的进度进行编写和放置,文件读取器将不得不到达文件的末尾,只获取匹配的密钥。我已经尝试了不同的方法,但到目前为止,没有什么东西接近我将使用java.util.Properties的结果。任何想法?

解决方案

这将逐行读取您的属性文件,并解析每个输入行,并将值键/值图。

 封装示例;包中的每个键都是唯一的(重复键不被允许)。 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.TreeMap;

public class ReadProperties {

public static void main(String [] args){
try {
TreeMap< String,String> map = getProperties(./ sample.properties);
System.out.println(map);

catch(IOException e){
//使用文件错误
}
}

public static TreeMap< String,String> ; getProperties(String infile)抛出IOException {
final int lhs = 0;
final int rhs = 1;

TreeMap< String,String> map = new TreeMap< String,String>();
BufferedReader bfr = new BufferedReader(new FileReader(new File(infile)));

字符串行; $()
while((line = bfr.readLine())!= null){
if(!line.startsWith(#)&&!line.isEmpty()){
String [] pair = line.trim()。split(=);
map.put(pair [lhs] .trim(),pair [rhs] .trim());
}
}

bfr.close();

return(map);




$ p $输出结果如下:

  {playerBoots = boot109,playerBottomArmor = armor457,playerClass = Warlock,playerHelmet = empty,playerOrigin = Newlands,playerUpperArmor = armor900} 
map.get(key string); 来访问每个地图元素

编辑:此代码不会检查格式不正确或缺少的=字符串。您可以通过检查对数组的大小来自己添加分隔符。


I'm building a RMI game and the client would load a file that has some keys and values which are going to be used on several different objects. It is a save game file but I can't use java.util.Properties for this (it is under the specification). I have to read the entire file and ignore commented lines and the keys that are not relevant in some classes. These properties are unique but they may be sorted in any order. My file current file looks like this:

# Bio
playerOrigin=Newlands
playerClass=Warlock
# Armor
playerHelmet=empty
playerUpperArmor=armor900
playerBottomArmor=armor457
playerBoots=boot109
etc

These properties are going to be written and placed according to the player's progress and the filereader would have to reach the end of file and get only the matched keys. I've tried different approaches but so far nothing came close to the results that I would had using java.util.Properties. Any idea?

解决方案

This will read your "properties" file line by line and parse each input line and place the values in a key/value map. Each key in the map is unique (duplicate keys are not allowed).

package samples;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.TreeMap;

public class ReadProperties {

    public static void main(String[] args) {
        try {
           TreeMap<String, String> map = getProperties("./sample.properties");
           System.out.println(map);
        }
        catch (IOException e) {
            // error using the file
        }
    }

    public static TreeMap<String, String> getProperties(String infile) throws IOException {
        final int lhs = 0;
        final int rhs = 1;

        TreeMap<String, String> map = new TreeMap<String, String>();
        BufferedReader  bfr = new BufferedReader(new FileReader(new File(infile)));

        String line;
        while ((line = bfr.readLine()) != null) {
            if (!line.startsWith("#") && !line.isEmpty()) {
                String[] pair = line.trim().split("=");
                map.put(pair[lhs].trim(), pair[rhs].trim());
            }
        }

        bfr.close();

        return(map);
    }
}

The output looks like:

{playerBoots=boot109, playerBottomArmor=armor457, playerClass=Warlock, playerHelmet=empty, playerOrigin=Newlands, playerUpperArmor=armor900}

You access each element of the map with map.get("key string");.

EDIT: this code doesn't check for a malformed or missing "=" string. You could add that yourself on the return from split by checking the size of the pair array.

这篇关于读取文件并获取key = value而不使用java.util.Properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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