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..?
Scanner fileScanner = new Scanner(myFile); fileScanner. nextLine(); This will return the first line of text from the file and discard it because you don't store it anywhere.
Java 8 has added a new method called lines() in the Files class which can be used to read a file line by line in Java. The beauty of this method is that it reads all lines from a file as Stream of String, which is populated lazily as the stream is consumed.
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!
You can opt to try using Iterator
Iterator<String> iter = Files.lines(Paths.get(schemaFileDir+File.separator+schemaFileNm)).iterator();
while (iter.hasNext()) {
iter.next(); // discard 1st line
sch.addRow(iter.next().toString(),file_delim); // process
}
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