I have a file format for potentially large files where the first line is special. I'd like to open the file once and treat it as a stream of lines, but handle the first line differently from all of the other lines. The others get map/collected, the first line needs to be just parsed into some flags. Is there a way?
This starts as:
result = Files.lines(path).map(something).collect(Collectors.toList());
except that I want to divert the first line.
If you must only open the file once, the easiest thing to do is to make a BufferedReader
, get the first line and then stream over the rest:
BufferedReader reader = Files.newBufferedReader(path);
String firstLine = reader.readLine();
result = reader.lines()
.map(something)
.collect(toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With