[英]How can I get an array containing all the Keys from a properties file in the correct sequence?
I have the following .properties file: 我有以下.properties文件:
0.0=value1
0.0 =值1
2.3=value2
2.3 =值2
2.45=value3
2.45 =值3
2.65=value4
2.65 = VALUE4
2.71=value5
2.71 =值5
2.87=value6
2.87 = value6
3.66=value7
3.66 = value7
I want to get an array with the keys from this file, something containing this: 我想从该文件中获取包含键的数组,其中包含以下内容:
[0.0, 2.3, 2.45, 2.65, 2.71, 2.87, 3.66]
I tried this: 我尝试了这个:
Properties prop = new Properties();
FileInputStream myInputStream;
myInputStream = new FileInputStream("./test.properties");
prop.load(myInputStream);
myInputStream.close();
Set set = prop.keySet();
Object vetKey[] = set.toArray();
But my set does not contain the keys in the order they appear in the file, 但是我的集合中没有按它们在文件中出现的顺序包含键,
I don't know the reason, they appear like this: 我不知道原因,它们看起来像这样:
[2.71, 3.66, 2.45, 2.3, 0.0, 2.65, 2.87]
[2.71、3.66、2.45、2.3、0.0、2.65、2.87]
How can I get an array with the correct sequence of keys? 如何获得具有正确键序列的数组? Like they are in the .properties :?
就像它们在.properties中一样 :
How about using a BufferedReader
instead. 如何使用
BufferedReader
代替。 Read each line into a string then use string.split
to split each line at the '=' signs. 将每一行读取为一个字符串,然后使用
string.split
在“ =”符号处分割每一行。 Add array[0]
from the string.split to your properties array. 将string.split中的
array[0]
添加到属性数组中。 They'll be in order. 他们会有秩序的。 Not the best way to do this, but it will get you what you want.
这不是最好的方法,但是它将为您提供所需的东西。
Arrays.sort();
or Collections.sort();
或
Collections.sort();
Have you tried using prop.keys() rather than prop.keySet()? 您是否尝试过使用prop.keys()而不是prop.keySet()? There's a difference between wanting them in the order that they occur in the properties file and having them in sorted order.
希望它们按照在属性文件中出现的顺序与按照排序的顺序排列是有区别的。 If you want them sorted, then sort them using Collections.sort()
如果要对它们进行排序,请使用Collections.sort()对它们进行排序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.