5

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.

Misha
  • 27,433
  • 6
  • 62
  • 78
bmargulies
  • 97,814
  • 39
  • 186
  • 310

1 Answers1

9

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());
Misha
  • 27,433
  • 6
  • 62
  • 78