我有一个以 int[]
作为输入的方法。
methodABC(int[] nValue)
我想从 Java 属性文件中获取此 nValue
nValue=1,2,3
我如何从配置文件中读取它,或者我是否必须以不同的格式存储它?
我尝试的是(将 nValue 更改为 123 而不是 1,2,3
):
int nValue = Integer.parseInt(configuration.getProperty("nnValue"));
我们如何做到这一点?
最佳答案
原始属性文件太90年代了:)你应该使用json文件,
无论如何:
如果你有这个:
nValue=1,2,3
然后读取 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));
}
从 java 8 开始:
int[] values = Stream.of(property.split(",")).mapToInt(Integer::parseInt).toArray();
for (int i : values) {
System.out.println(i);
}
关于java - 从 java 属性文件中读取 int[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43338223/