简体   繁体   中英

List of properties in a properties files

Usually a Java properties files stores key,value pairs. 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.

  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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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