Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedReader to skip first line

I am using the following bufferedreader to read the lines of a file,

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

Now, I want to skip reading the first line of the file and I don't want to use a counter line int lineno to keep a count of the lines.

How to do this?

like image 824
Code Avatar asked Sep 06 '25 10:09

Code


2 Answers

You can try this

 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
 }
like image 147
Ruchira Gayan Ranaweera Avatar answered Sep 09 '25 18:09

Ruchira Gayan Ranaweera


You can use the Stream skip() function, like this:

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

lines.forEachOrdered(line -> {

...
});
like image 21
Giovanni Cosentino Avatar answered Sep 09 '25 19:09

Giovanni Cosentino