Winston Gutkowski wrote:. . . and on version 8 maybe even Streams.
Winston
Do you mean this sort of thing?
OP: You should by now be familiar with creating a reader with
try‑with‑resources.
The reader's
lines() method returns a
Stream<String> where each
String represents one line. Since Streams run on lazy execution, calling
limit(12L) stops the Stream after the 12th line has been read, if there are more than 12 lines in the first place..
You can collect things with the
collect() method. That reduces the Stream to one value, which in this case will be a String.
The argument is
(Collectors)#joining() which “Returns a
Collector that concatenates the input elements into a String, in encounter order.” This is similar to using a
StringJoiner.
In this instance, joining() causes the individual Strings in the Stream to be joined together, separated by the String ",%n" which is a comma followed by the “the platform-specific line separator as returned by System.getProperty("line.separator").” which you can find
here with ctrl‑F‑"'n'".