0

通常,Java 属性文件存储键、值对。但是,在外部属性文件中仅将字符串列表作为属性存储的最佳方法是什么?

4

2 回答 2

4

如果您只想存储字符串列表,则不需要属性文件。

  1. 您可以将密钥以逗号分隔的形式存储在文本文件中。当您想访问它们时,只需阅读完整的文件并使用逗号分割

  2. 另一种选择是您可以将所有键存储在一个文本文件中,以便每个键都在一行上。然后您可以使用FileUtils.readLines(File file)来获取所有键的列表。

  3. 如果您仍然想将它们存储在属性文件中,那么您可以只存储键,而不存储任何值。然后使用propertyNames获取所有键的列表。

于 2013-04-28T06:46:50.890 回答
0

You can store a comma separated list in a value and use split("\s*,\s*") method to separate them.

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

List<String> lines = new ArrayList<>();
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
    for(String line; (line = br.readLine()) != null;)
        lines.add(line);
}
于 2013-04-28T06:35:30.323 回答