Reputation: 8923
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 ?
Upvotes: 0
Views: 200
Reputation: 533530
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);
}
Upvotes: 0
Reputation: 3753
If you just want to store list of strings, then you don't need properties file.
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
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.
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.
Upvotes: 4