Bet*_*ish 0 java arrays parsing properties-file
我有一个int[]输入的方法.
methodABC(int[] nValue)
Run Code Online (Sandbox Code Playgroud)
我想从Java属性文件中获取此nValue
nValue=1,2,3
Run Code Online (Sandbox Code Playgroud)
如何从配置文件中读取此内容,还是以其他格式存储?
我尝试的是(changing the nValue to 123 instead of 1,2,3):
int nValue = Integer.parseInt(configuration.getProperty("nnValue"));
Run Code Online (Sandbox Code Playgroud)
我们如何做到这一点?
原始属性文件是如此90的:)你应该使用json文件,
无论如何:
如果你有这个:
nValue=1,2,3
Run Code Online (Sandbox Code Playgroud)
然后读取nValue,将其拆分为逗号并将流/循环解析为int
String property = prop.getProperty("nValue");
System.out.println(property);
String[] x = property.split(",");
for (String string : x) {
System.out.println(Integer.parseInt(string));
}
Run Code Online (Sandbox Code Playgroud)
从java 8开始:
int[] values = Stream.of(property.split(",")).mapToInt(Integer::parseInt).toArray();
for (int i : values) {
System.out.println(i);
}
Run Code Online (Sandbox Code Playgroud)