简体   繁体   English

使用相同的多个键读取.txt(属性)文件-Java

[英]read .txt (properties) file with same multiple keys - java

In simple java terms, with out using any other libraries (apache), how to i read below lines : 用简单的Java术语,不使用任何其他库(Apache),我如何阅读以下内容:

data.txt
    CheckUpdate:  110
    CheckTechNotes:  11
    CheckUpdate:  220
    CheckTechNotes:  21
    CheckUpdate:  330
    CheckTechNotes:  31

Used below code: 用于以下代码:

public class PropertiesFile {
    public static void main(String[] args) {
        Properties prop = new Properties();
        InputStream input = null;

        try {
            input = new FileInputStream("data.txt");
            prop.load(input);
            Enumeration<?> e = prop.propertyNames();

            while (e.hasMoreElements()) {
                System.out.println(prop.getProperty("CheckUpdate"));
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

Always returning 330, how do i get 110, 220 and 330 values ? 总是返回330,我如何获得110、220和330的值?

Note: it's like having 3 employee details in a file, how do i read those 3 employee objects? 注意:这就像在文件中包含3个员工详细信息,我该如何读取这3个员工对象?

Please suggest. 请提出建议。

thanks 谢谢

Property files contain a set of key-value pairs, so it does not make sense to have duplicate keys. 属性文件包含一组键值对,因此具有重复的键没有意义。 The propertyName() call will give you only distinct keys. 调用propertyName()只会给您唯一的键。 Just open the file with a normal FileInputStream and scan through your entries instead. 只需使用普通的FileInputStream打开文件,然后浏览您的条目即可。

您可以在读取List<String>时添加这些属性,然后以myList.add(prop.getProperty("CheckUpdate"));访问它们myList.add(prop.getProperty("CheckUpdate"));

You likely want to use a structured file format like JSON rather than a flat file format like properties for this type of data. 您可能希望使用诸如JSON之类的结构化文件格式,而不是针对此类数据使用诸如属性之类的平面文件格式。 There are many utility libraries available to de-serialize the data such as gson. 有许多实用程序库可用于反序列化数据,例如gson。

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

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