Saturday, December 30, 2017

How to read nested properties from configuration file in java

Leave a Comment

I want to read Name property from configuration file

config.properties

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

I tried

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

But this function is not able to read nested properties.

Please help me find the solution.

3 Answers

Answers 1

your file config.properties structure looks like json format and to my understanding you want to parse a json file, correct me if i am wrong. To read or write from a json objects u can use Gson like this

my json file

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

My POJO classes includes

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

read the file convert to string and then read json object using Gson.

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

Answers 2

It is not legal properties file. But it is legal HOCON conf.

An example of your conf file reading with hocon generated by tscfg.

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

Answers 3

In order to directly answer your question, you are not doing a proper config.properties file.

normally, a property file would look like this as per your example:

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

In your code, you should be fine by just doing the following after ***props.load(reader)*** (Note I am not copying all your code.)

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

Here I am simply outputting the results but note you can do whatever you wish to do.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment