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

davymartu
  • 1,393
  • 3
  • 15
  • 34
  • 1
    You can have some help from [apache configuration](http://commons.apache.org/configuration/) and retrieve a string array `configuration.getAsStringArray("key")` – Paizo Jan 05 '16 at 15:29
  • What do you expect this code to do and what does it actually do? – Will Sheppard Jan 05 '16 at 15:47
  • See [this thread](http://stackoverflow.com/questions/7015491/better-way-to-represent-array-in-java-properties-file). If you have to configure arrays in a configuration file, the best option to go is XML. – Henrique Barcelos Jan 05 '16 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. http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/ – Omid Jan 16 '16 at 23:32

1 Answers1

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.

Community
  • 1
  • 1
Omid
  • 5,823
  • 4
  • 41
  • 50