【问题标题】:How to read nested properties from configuration file in java如何从java中的配置文件中读取嵌套属性
【发布时间】:2018-06-03 18:42:08
【问题描述】:

我想从配置文件中读取 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;
}

但是这个函数只能读取键值对并且它不能读取嵌套的属性。那么我应该使用什么来读取嵌套属性呢?

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

【问题讨论】:

  • 我建议您为给定的结构使用YAML 文件而不是property 文件。
  • 您是如何认为上述是可接受的 .properties 文件格式?它不是。这里是文件格式的说明:docs.oracle.com/javase/8/docs/api/java/util/…如果你想要像上面这样的分层属性,你需要编写自己的解析器。
  • 使用 Groovy 并为此创建一个 DSL。这种结构非常适合 Groovy。

标签: java


【解决方案1】:

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

使用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】:

    您的文件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();
    

    【讨论】:

      【解决方案3】:

      为了直接回答你的问题,你没有做一个正确的 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
      

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

      【讨论】:

        猜你喜欢
        • 2016-09-08
        • 1970-01-01
        • 2021-11-09
        • 1970-01-01
        • 2011-03-14
        • 2012-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多