BufferedReader to skip first line
Asked Answered
O

6

28

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?

Ornament answered 23/4, 2014 at 6:1 Comment(4)
Why don't you readLine and ignore it?Shipwreck
Just call reader.readLine() before the loop.Hoenack
possible duplicate of Skipping over the first line when reading a textfileCounterpunch
@AndrewLogvinov Thank you! I've used your method and seems to be the most efficient way possible, please post it as an answer.Ornament
S
67

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
 }
Somersault answered 23/4, 2014 at 6:4 Comment(2)
Simple. How come I didn't thought of that? :DQuantic
I feel so dumb now :DShannan
A
18

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

...
});
Aspergillus answered 19/11, 2020 at 18:1 Comment(4)
While this code may solve the question, including an explanation 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.Leyte
this one is perfect.Suetonius
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 errorBroadcloth
@Broadcloth the chosen error one can be resolved with try (BufferedReader reader = new BufferedReader(new FileReader(somepath))) { ..... code }, not big dealAllbee
T
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
Taligrade answered 27/11, 2015 at 6:25 Comment(2)
Adding comments on how your code works would help users to better understand your code.Kiowa
Hope my code is more clear now with the comments. Thanks for the warning ;)Taligrade
E
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);
            }
Eraser answered 23/4, 2014 at 6:9 Comment(5)
This is quite possibly the most inefficient and over-complicated way to solve this problem.Counterpunch
@BrianRoach Why do you think linenumberreader is over complicated?Eraser
Because A) it's not necessary and B) you're doing a unnecessary comparison on every read.Counterpunch
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?Eraser
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.Counterpunch
C
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++;
}
Convex answered 23/4, 2014 at 6:12 Comment(0)
C
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();
Cutler answered 30/11, 2018 at 16:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.