11

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

4

3 回答 3

23

使用Stream.skip方法跳过标题行。

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

希望这可以帮助!

于 2016-12-21T08:00:10.857 回答
1

您可以选择尝试使用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
}
于 2016-12-21T08:01:19.380 回答
-1

问题是:你为什么要这样做?

我的猜测是您正在阅读 CSV 文件。在这种情况下,您很快就会遇到其他问题,例如如何区分字符串和数字?或者如何处理“”中的双引号、分号或逗号?

我的建议是通过使用 CSV 阅读器框架来解析文件,从一开始就避免所有这些麻烦。

于 2016-12-21T08:11:37.680 回答