简体   繁体   English

如何以正确的顺序从属性文件中获取包含所有键的数组?

[英]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.

相关问题 我如何从Android中的数组contentValue获取(键,值) - How can i get (keys,values) from an array contentvalue in android 如何从Spring Boot Config Server服务的其他属性文件中的application.properties获取密钥? - How can I get the keys from application.properties within other properties files served by Spring Boot Config Server? 将属性文件中的键读入数组 - Read keys from Properties file into array 如何在intellij的属性文件中找到我可以使用的所有属性 - How to find all properties that i can use in the properties file in intellij 如何从扫描仪输入到数组获取所有排列? - How can I get all the permutations from a scanner input to array? 如何将字符串序列和字节数组写入文件? - How can I write a sequence of strings and then a byte array to a file? 我如何从一个看起来像 json 字符串的字符串中获取所有密钥 - How i can get all the keys from a string which somehow looks like json string 我怎样才能按照他们在文件中的顺序获取属性 - How can i get the property in the sequence they have in file 如何获得前缀为“abc”的所有属性。 来自PropertyPlaceholderConfigurer - How can I get all properties with prefix 'abc.' from PropertyPlaceholderConfigurer 如何获得PropertyPlaceHolderConfigurer中所有属性的列表? - How can I get a list of all properties in PropertyPlaceHolderConfigurer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM