Using Java 8 NIO, how can I read a file while skipping the first line or header record?

vhora picture vhora · Dec 21, 2016 · Viewed 7.6k times · Source

I am trying to read a large file line by line, in java, using NIO library. But this file also contains headers... try (Stream<String> stream = Files.lines(Paths.get(schemaFileDir+File.separator+schemaFileNm))){ stream.forEach(s->sch.addRow(s.toString(),file_delim)); }

How do i modify this to skip the first line of the file? Any pointers..?

Answer

anacron picture anacron · Dec 21, 2016

Use the Stream.skip method to skip the header line.

try (Stream<String> stream = Files.lines(
          Paths.get(
             schemaFileDir+File.separator+schemaFileNm)).skip(1)){
 // ----
}

Hope this helps!