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

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

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

How to exercise this?

asked Apr 23, 2014 at six:01

4

  • Why don't you readLine and ignore it?

    April 23, 2014 at 6:04

  • Just telephone call reader.readLine() earlier the loop.

    April 23, 2014 at half dozen:04

  • @AndrewLogvinov Thank you! I've used your method and seems to be the well-nigh efficient way possible, please post it every bit an answer.

    April 23, 2014 at 6:xix

6 Answers 6

You can endeavor this

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

answered Apr 23, 2014 at 6:04

1

  • Simple. How come I didn't thought of that? :D

    Oct 6, 2019 at nineteen:52

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

                  BufferedReader reader = new BufferedReader(new FileReader(somepath));    Stream<String> lines = reader.lines().skip(ane);  lines.forEachOrdered(line -> {  ... });                                  

answered Nov 19, 2020 at eighteen:01

2

  • While this code may solve the question, including an explanation of how and why this solves the problem would actually aid to improve the quality of your mail service, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not simply the person request at present. Please edit your respond to add explanations and requite an indication of what limitations and assumptions apply.

    Nov 19, 2020 at 18:10

  • this one is perfect.

    Mar v, 2021 at 20:34

                  File file = new File("path to file"); FileInputStream fis = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); Cord line = null; int count = 0; while((line = br.readLine()) != null) { // read through file line by line     if(count != 0) { // count == 0 means the outset line         System.out.println("That's not the first line");     }     count++; // count increments as you read lines } br.close(); // do not forget to close the resources                                  

answered Nov 27, 2015 at 6:25

2

  • Calculation comments on how your code works would help users to amend sympathize your code.

    Nov 27, 2015 at half dozen:49

  • Promise my code is more clear at present with the comments. Thanks for the warning ;)

    November 27, 2015 at seven:15

Utilise a linenumberreader instead.

                  LineNumberReader reader = new LineNumberReader(new InputStreamReader(file.getInputStream()));             String line1;             while ((line1 = reader.readLine()) != null)              {                 if(reader.getLineNumber()==ane){                     keep;                 }                 System.out.println(line1);             }                                  

answered Apr 23, 2014 at 6:09

5

  • This is quite possibly the most inefficient and over-complicated way to solve this problem.

    April 23, 2014 at 6:10

  • @BrianRoach Why do you recollect linenumberreader is over complicated?

    Apr 23, 2014 at 6:12

  • Because A) information technology's not necessary and B) y'all're doing a unnecessary comparison on every read.

    Apr 23, 2014 at 6:xiv

  • A) It gives you control to skip any line y'all wish. Not limited to the first line. B) I didn't come across any performance issue related remark in the question. I hope the user will definitely customize the solution and not copy paste it. In fact even the InputstreamReader is non required here, fileReader will practise. Are we being also opinionated hither?

    Apr 23, 2014 at 6:19

  • My personal opinion is why build the space shuttle when all you need is a biplane. You lot want to skip the kickoff line in a file. Yous accept a BufferedReader. Phone call readLine() prior to the loop. Done.

    April 23, 2014 at 6:22

Y'all can create a counter that contains the value of the starting line:

                private concluding static START_LINE = one;  BufferedReader reader = new BufferedReader(new FileReader(somepath)); int counter=START_LINE;  while ((line1 = reader.readLine()) != aught) {   if(counter>START_LINE){      //your code here   }   counter++; }                              

Andremoniy

32.4k 15 gold badges 121 silverish badges 227 statuary badges

answered Apr 23, 2014 at 6:12

Yous can do it similar this:

                BufferedReader buf = new BufferedReader(new FileReader(fileName));             Cord line = zip;             String[] wordsArray;             boolean skipFirstLine = true;   while(true){                 line = buf.readLine();                 if ( skipFirstLine){ // skip information header                     skipFirstLine = false; continue;                 }                 if(line == aught){                       break;                  }else{                     wordsArray = line.split("\t"); } buf.close();                              

answered Nov 30, 2018 at sixteen:05

Non the answer yous're looking for? Browse other questions tagged java bufferedreader or ask your ain question.