I have a method using the code below based on other resources how to iterate a file using a Java 8 stream:
String result = "";
try (Stream<String> stream = Files.lines(Paths.get("/home/user/file.txt"))) {
stream.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
However, I want to save the lines to += a string, instead of console.
I have tried these 2 below but neither pass IDE validation:
stream.forEach(thisString -> result += thisString);
stream.forEach(result += this);
stream.forEach(result::this);
What is the proper way to append each result from the foreach loop to a String object?