如何从java中的配置文件中读取嵌套属性

时间:2017-12-21 06:16:51

标签: java

我想从配置文件中读取Name属性。我希望我的配置文件看起来像这样:

config.properties

Person{
  Name= ABC
  PhoneNumber=123
      }
Address{     
  Pin=500
       } 

我试过

public Properties readConfiguration()
{
    File configFile1 = new File("config.properties");

    try {
        FileReader reader = new FileReader(configFile1);
        Properties props = new Properties();
        props.load(reader);

        reader.close();
        return props;
    } catch (FileNotFoundException ex) {
        // file does not exist
    } catch (IOException ex) {
        // I/O error
    }

    return null;
}

但是这个函数只能读取键值对,而且无法读取嵌套属性。那么我应该用什么来读取嵌套属性呢?

请帮我找到读取嵌套属性的解决方案。

3 个答案:

答案 0 :(得分:2)

您的文件config.properties结构看起来像json格式,根据我的理解,您想解析一个json文件,如果我错了,请更正我。要从json对象读取或写入,您可以使用Gson这样的

我的json文件

{
    Person:{
     "Name" = "ABC",
      "PhoneNumber" = 123
      },
    Address: {
      "Pin" = 500
     }
}

我的POJO课程包括

    public class Person {
        private String Name;
        private  int PhoneNumber;

        public Person() {
        }

        public String getName() {
            return Name;
        }

        public void setName(String name) {
            Name = name;
        }

        public int getPhoneNumber() {
            return PhoneNumber;
        }

        public void setPhoneNumber(int phoneNumber) {
            PhoneNumber = phoneNumber;
        }
    }
    public class  Address{
            private String Pin;

            public Address() {
            }

            public String getPin() {
                return Pin;
            }

            public void setPin(String pin) {
                Pin = pin;
            }
        }
public class Config{
        private Person person;
        private Address address;

    public Config() {
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

读取文件转换为字符串,然后使用Gson读取json对象。

 Scanner scanner = new Scanner(new File("config.properties"));
        String fileStr="";
        while (scanner.hasNextLine()){
            fileStr+=scanner.nextLine();
        }
        Gson gson = new Gson();
        Config config =gson.fromJson(fileStr,Config.class);
        config.getPerson.getPhoneNumber();

答案 1 :(得分:1)

它不是合法的属性文件。 但这是合法的HOCON conf。

使用tscfg生成的hocon读取conf文件的示例。

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.junit.Assert;

import java.io.File;

public class SampleConf {
    public final SampleConf.Address Address;
    public final SampleConf.Person Person;

    public static class Address {
        public final int Pin;

        public Address(final com.typesafe.config.Config c) {
            this.Pin = c.hasPathOrNull("Pin") ? c.getInt("Pin") : 500;
        }
    }

    public static class Person {
        public final java.lang.String Name;
        public final int PhoneNumber;

        public Person(final com.typesafe.config.Config c) {
            this.Name = c.hasPathOrNull("Name") ? c.getString("Name") : "ABC";
            this.PhoneNumber = c.hasPathOrNull("PhoneNumber") ? c.getInt("PhoneNumber") : 123;
        }
    }

    public SampleConf(final com.typesafe.config.Config c) {
        this.Address = new SampleConf.Address(c.getConfig("Address"));
        this.Person = new SampleConf.Person(c.getConfig("Person"));
    }

    public static void main(final String[] args) {

        final Config config = ConfigFactory.parseFile(
                new File(("problem.so.hocon.conf"))) // file path
                .resolve();
        final SampleConf conf = new SampleConf(config);
        // do everything  you like
        final SampleConf.Address address = conf.Address;
        final SampleConf.Person person = conf.Person;
        Assert.assertEquals(500, address.Pin);
    }

}

答案 2 :(得分:0)

为了直接回答您的问题,您没有正确执行config.properties文件。

通常,根据您的示例,属性文件看起来像这样:

//File 
Name=ABC
Phonenumber=123456
pin=1234

在你的代码中,只需在***props.load(reader)***后执行以下操作就可以了(注意我没有复制你的所有代码。)

System.out.println(props.getProperty("Name")); // ABC
System.out.println(props.getProperty("Phonenumber")); // 123456
System.out.println(props.getProperty("pin")); // 123

这里我只是输出结果,但请注意你可以做任何你想做的事。