3

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

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

这些属性将根据播放器的进度写入和放置,文件读取器必须到达文件末尾并仅获取匹配的键。我尝试了不同的方法,但到目前为止,没有什么能接近我使用 java.util.Properties 获得的结果。任何的想法?

4

5 回答 5

5

这将逐行读取您的“属性”文件并解析每个输入行并将值放在键/值映射中。映射中的每个键都是唯一的(不允许重复键)。

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

输出如下所示:

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

您可以使用 访问地图的每个元素map.get("key string");

编辑:此代码不检查格式错误或丢失的“=”字符串。您可以通过检查对数组的大小在从拆分返回时自己添加它。

于 2012-02-15T20:45:21.957 回答
2

我目前无法想出一个框架来提供它(我相信有很多),但是,你应该能够自己做。

基本上,您只需逐行读取文件并检查第一个非空白字符是否为哈希 ( #) 或该行是否仅为空白。您会忽略这些行并尝试将其他行拆分为=. 如果对于这样的拆分,您没有得到一个包含 2 个字符串的数组,那么您有一个格式错误的条目并相应地处理它。否则,第一个数组元素是您的键,第二个是您的值。

于 2012-02-15T20:19:12.750 回答
1

或者,您可以使用正则表达式来获取键/值对。

(?m)^[^#]([\w]+)=([\w]+)$

将为每个键及其值返回捕获组,并忽略注释行。

编辑:

这可以简单一点:

[^#]([\w]+)=([\w]+)
于 2012-02-15T20:44:51.863 回答
1

经过一番研究,我想出了这个解决方案:

public static String[] getUserIdentification(File file) throws IOException {
        String key[] = new String[3];
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String lines;
        try {
            while ((lines = br.readLine()) != null) {
                String[] value = lines.split("=");
                if (lines.startsWith("domain=") && key[0] == null) {
                    if (value.length <= 1) {
                        throw new IOException(
                                "Missing domain information");
                    } else {
                        key[0] = value[1];
                    }
                }

                if (lines.startsWith("user=") && key[1] == null) {
                    if (value.length <= 1) {
                        throw new IOException("Missing user information");
                    } else {
                        key[1] = value[1];
                    }
                }

                if (lines.startsWith("password=") && key[2] == null) {
                    if (value.length <= 1) {
                        throw new IOException("Missing password information");
                    } else {
                        key[2] = value[1];
                    }
                } else
                    continue;
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return key;
}

我正在使用这段代码来检查属性。当然使用属性库会更明智,但不幸的是我不能。

于 2012-02-17T18:25:55.630 回答
0

更短的方法是如何做到这一点:

    Properties properties = new Properties();
    String confPath = "src/main/resources/.env";
    
    try {
        properties.load(new FileInputStream(confPath));
    } catch (IOException e) {
        e.printStackTrace();
    }

    String specificValueByKey = properties.getProperty("KEY");
    Set<Object> allKeys = properties.keySet();
    Collection<Object> values = properties.values();
于 2022-01-15T11:53:45.650 回答