Reputation: 424
Each line in the file has comma seperated data.I need to extract each field based on comma from each row as String and store as property. How to achieve that using Java Streams?
Stream<String> lines = Files.lines(wiki_path);
Upvotes: 4
Views: 345
Reputation: 44398
Read from the file:
Files::lines
returns a sequence of lines Stream<String>
which might be used for your advantage.
List<String> propertyList = Files.lines(wiki_path) // Stream<String>
.map(line ->line.split(";")) // Stream<String[]>
.flatMap(Arrays::stream) // Stream<String>
.collect(Collectors.toList()); // List<String>
The result is a List
of all the properties read from the file separated with a new line and the ;
delimiter.
Write to Properties:
Since the properties are built on the key-value principle, you need the key for each loaded property. Let's suppose the value = key:
Properties properties = new Properties();
propertyList.forEach(property -> properties.set(property, property ));
This is rather a very primitive example and you have to amend it to your needs. Read more about properties at Properties.
Upvotes: 2