简体   繁体   English

属性文件中的属性列表

[英]List of properties in a properties files

Usually a Java properties files stores key,value pairs. 通常,Java属性文件存储键,值对。 But what is the best way to store only a list of strings as properties in an external properties files ? 但是,仅将字符串列表作为属性存储在外部属性文件中的最佳方法是什么?

If you just want to store list of strings, then you don't need properties file. 如果只想存储字符串列表,则不需要属性文件。

  1. You can store the keys as comma separated in a text file. 您可以将密钥以逗号存储在文本文件中。 When you want to access them, just read the complete file and split using comma 当您要访问它们时,只需阅读完整的文件并使用逗号分割即可

  2. Another option is you can store all the keys in a text file so that every key is on one line. 另一个选择是,您可以将所有键存储在文本文件中,以便每个键都在一行上。 Then you can use FileUtils.readLines(File file) to get the list of all keys. 然后,您可以使用FileUtils.readLines(File file)获取所有键的列表。

  3. If still you want to store them in properties file, then you can store only keys, without any values. 如果仍然要将它们存储在属性文件中,则只能存储键,而没有任何值。 Then use propertyNames to get the list of all keys. 然后使用propertyNames获取所有键的列表。

You can store a comma separated list in a value and use split("\\s*,\\s*") method to separate them. 您可以将逗号分隔的列表存储在一个值中,并使用split(“ \\ s *,\\ s *”)方法将其分隔。

key=value1, value2, value3

Or if all you need is a list of values, Properties is not appropriate as the order of keys is not preserved. 或者,如果您只需要一个值列表,则由于不保留键的顺序,因此不适合使用“属性”。 You can have a text file with one line per value 您可以有一个文本文件,每个值一行

value1
value2
value3

You can use a BufferedReader like this 您可以像这样使用BufferedReader

List<String> lines = new ArrayList<>();
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
    for(String line; (line = br.readLine()) != null;)
        lines.add(line);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM