26

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?

Code
  • 785
  • 3
  • 8
  • 19

6 Answers6

65

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
 }
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
11

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 -> {

...
});
  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Dharman Nov 19 '20 at 18:10
  • 2
    this one is perfect. – Sukh Mar 05 '21 at 20:34
  • worth mention that this solution will not trigger errors on code analysis software like SonaQube, while the chosen one will throw an error a bug error – JrBenito Aug 04 '22 at 04:16
  • @JrBenito the chosen error one can be resolved with `try (BufferedReader reader = new BufferedReader(new FileReader(somepath))) { ..... code }`, not big deal – Al-Mothafar Feb 19 '23 at 15:19
5
File file = new File("path to file");
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
int count = 0;
while((line = br.readLine()) != null) { // read through file line by line
    if(count != 0) { // count == 0 means the first 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
user3743369
  • 61
  • 1
  • 4
2

Use a linenumberreader instead.

LineNumberReader reader = new LineNumberReader(new InputStreamReader(file.getInputStream()));
            String line1;
            while ((line1 = reader.readLine()) != null) 
            {
                if(reader.getLineNumber()==1){
                    continue;
                }
                System.out.println(line1);
            }
Hirak
  • 3,601
  • 1
  • 22
  • 33
  • 2
    This is quite possibly the most inefficient and over-complicated way to solve this problem. – Brian Roach Apr 23 '14 at 06:10
  • @BrianRoach Why do you think linenumberreader is over complicated? – Hirak Apr 23 '14 at 06:12
  • 1
    Because A) it's not necessary and B) you're doing a unnecessary comparison on every read. – Brian Roach Apr 23 '14 at 06:14
  • 1
    A) It gives you control to skip any line you wish. Not limited to the first line. B) I didn't see 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 not required here, fileReader will do. Are we being too opinionated here? – Hirak Apr 23 '14 at 06:19
  • 3
    My *personal* opinion is why build the space shuttle when all you need is a biplane. You want to skip the first line in a file. You have a `BufferedReader`. Call `readLine()` prior to the loop. Done. – Brian Roach Apr 23 '14 at 06:22
1

You can create a counter that contains the value of the starting line:

private final static START_LINE = 1;

BufferedReader reader = new BufferedReader(new FileReader(somepath));
int counter=START_LINE;

while ((line1 = reader.readLine()) != null) {
  if(counter>START_LINE){
     //your code here
  }
  counter++;
}
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
richersoon
  • 4,682
  • 13
  • 44
  • 74
0

You can do it like this:

BufferedReader buf = new BufferedReader(new FileReader(fileName));
            String line = null;
            String[] wordsArray;
            boolean skipFirstLine = true;


while(true){
                line = buf.readLine();
                if ( skipFirstLine){ // skip data header
                    skipFirstLine = false; continue;
                }
                if(line == null){  
                    break; 
                }else{
                    wordsArray = line.split("\t");
}
buf.close();
Largo_code
  • 31
  • 2