0

I have method which takes an int[] as input.

methodABC(int[] nValue)

I would like to take this nValue from a Java property file

nValue=1,2,3

How do I read this from the config file or do I have to store this in a different format?

What I tried is (changing the nValue to 123 instead of 1,2,3):

int nValue = Integer.parseInt(configuration.getProperty("nnValue"));

How do we do this?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Betafish
  • 1,212
  • 3
  • 20
  • 45
  • Not sure if duplicate, but this may be interesting for you: http://stackoverflow.com/questions/7015491/better-way-to-represent-array-in-java-properties-file – Hexaholic Apr 11 '17 at 06:27
  • If `nVAlueString="1,2,3";` you can do : `nValueString.split(",");` and then parse the resulting array. – jrook Apr 11 '17 at 06:28

5 Answers5

6

Raw-properties file is so 90`s :) you should use a json file instead,

anyways:

if you have this:

nValue=1,2,3

then read nValue, split that to comma and stream/loop parsing to int

example:

String property = prop.getProperty("nValue");
System.out.println(property);
String[] x = property.split(",");
for (String string : x) {
    System.out.println(Integer.parseInt(string));
}

since java 8:

int[] values = Stream.of(property.split(",")).mapToInt(Integer::parseInt).toArray();
for (int i : values) {
    System.out.println(i);
}
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
4

Use String.split and Integer.parseInt. With Streams you can do it in one line:

String property = configuration.getProperty("nnValue")
int[] values = Stream.of(property.split(",")).mapToInt(Integer::parseInt).toArray()
ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
1

You need to read the value (nValue=1,2,3) as string only and split the string (with ",") then convert to an int[] array as shown below:

//split the input string
String[] strValues=configuration.getProperty("nnValue").split(",");
int[] intValues = strValues[strValues.length];//declare int array
for(int i=0;i<strValues.length;i++) {
    intValues[i] = Integer.parseInt(strValues[i]);//populate int array
}

Now, you can call the method by passing the intValues array as shown below:

methodABC(intValues);
Vasu
  • 21,832
  • 11
  • 51
  • 67
1

If you use Spring, you can read directly arrays in you properties (but also maps, if you need more complex data). E.g. in application.properties:

nValue={1,2,3}

and in your code:

@Value("#{${nValue}}")
Integer[] nValue;

then you can use nValue where you want.

Sampisa
  • 1,487
  • 2
  • 20
  • 28
-2

Here is how to read property of Property file in java:

Properties prop = new Properties();
try {
    //load a properties file from class path, inside static method
    prop.load(App.class.getClassLoader().getResourceAsStream("config.properties"));

    //get the property value and print it out
    System.out.println(prop.getProperty("database"));
    System.out.println(prop.getProperty("dbuser"));
    System.out.println(prop.getProperty("dbpassword"));

} 
catch (IOException ex) {
    ex.printStackTrace();
}
Jay Smith
  • 2,331
  • 3
  • 16
  • 27