2

I have this object:

public class TheObjectToInstantiate{
 public String Name;
 public String Surname;
 public TheObjectToInstantiate(){
 }
}

I want to instantiate an array of TheObjectToInstantiate[] with configuration file:

TheObjectToInstantiate1.Name="Pippo"
TheObjectToInstantiate1.Surname="PippoSurname"
TheObjectToInstantiate2.Name="Pluto"
TheObjectToInstantiate2.Surname="PlutoSurname"

I've tried with

public ConfigReader(){
    Properties prop = new Properties();
    InputStream input = null;

    try {

        input = new FileInputStream("configuration.prop");
        prop.load(input);

        Enumeration<?> e = prop.propertyNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String value = prop.getProperty(key);
            ......
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

scanning all properties and instatiate object manually.

There are ways or open source wrapper to do this without manually compare all properties? Thanks

4
  • 1
    You can have some help from apache configuration and retrieve a string array configuration.getAsStringArray("key") Commented Jan 5, 2016 at 15:29
  • What do you expect this code to do and what does it actually do? Commented Jan 5, 2016 at 15:47
  • See this thread. If you have to configure arrays in a configuration file, the best option to go is XML. Commented Jan 5, 2016 at 16:35
  • I think it would be easier to use json files and deserialize them with libraries like Jackson or Gson specially if you expect to have more complex structures like nested objects or a List or array inside your primary object. mkyong.com/java/how-to-convert-java-object-to-from-json-jackson Commented Jan 16, 2016 at 23:32

1 Answer 1

1

It's easier to use json files and deserialize them with libraries like Jackson. You may also check http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson and How to use Jackson to deserialise an array of objects

public class TheObjectToInstantiate {
    public String Name;
    public String Surname;
    public TheObjectToInstantiate(){}
}

public class JacksonExample {
    public static void main(String[] args) {

    ObjectMapper mapper = new ObjectMapper();

    try {
        // Convert JSON string from file to Object
        TheObjectToInstantiate object = mapper.readValue(new File("G:\\myobject.json"), TheObjectToInstantiate.class);
    } catch (IOException e) {
            e.printStackTrace();
    }

}

json file would be like this:

{
  "Name" : "foo",
  "Surname" : "bar"
}

you can also deserialize a list of objects:

List<TheObjectToInstantiate> myObjects = mapper.readValue(new File("G:\\myobjectlist.json"), new TypeReference<List<TheObjectToInstantiate>>(){});


[{
    "Name" : "foo1",
    "Surname" : "bar1"
},
{
    "Name" : "foo2",
    "Surname" : "bar2"
}]

It also supports more complex structures like nested objects or a List or array of other objects inside your primary object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.