缓冲阅读器跳过第一行

2022-09-01 18:02:07

我正在使用以下内容来读取文件的行,bufferedreader

BufferedReader reader = new BufferedReader(new FileReader(somepath));
while ((line1 = reader.readLine()) != null) 
{
    //some code
}

现在,我想跳过阅读文件的第一行,我不想使用计数器行来保持行数。int lineno

如何做到这一点?


答案 1

你可以试试这个

 BufferedReader reader = new BufferedReader(new FileReader(somepath));
 reader.readLine(); // this will read the first line
 String line1=null;
 while ((line1 = reader.readLine()) != null){ //loop will run from 2nd line
        //some code
 }

答案 2

您可以使用 Stream skip() 函数,如下所示:

BufferedReader reader = new BufferedReader(new FileReader(somepath));   
Stream<String> lines = reader.lines().skip(1);

lines.forEachOrdered(line -> {

...
});

推荐