Search code examples
javafilebufferedreaderfilereader

Read every line out of a txt


Hey there im trying to read a .txt file line by line but somehow it only reads every second line..

try {
    FileReader fr = new FileReader("file.txt");
    BufferedReader br = new BufferedReader(fr);
    while (br.readLine() != null){
        println(br.readLine());     //method to print the line
    }
}catch (FileNotFoundException e){}

This is what it should print:

stuff
stuff
stuff
more stuff
SAVED
LOADED
SAVED
LOADED

Instead it just prints this:

stuff
more stuff
LOADED
LOADED

I have no idea whats going on and could really use some help


Solution

  • When calling br.readLine() you are already reading but your first call is just to make sure you have a return value which ist not null. Go with something like this:

    String line;
    while((line = br.readLine()) != null)
    {
       println(line);
    }