Search code examples
javajava-8java.nio.file

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


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..?


Solution

  • 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!