Hauke Dau
Hauke Dau

Reputation: 21

why Java buffered reader missed a lot of lines in output large txt file as input?

Hi I using following code

public class Readfiles {

    FileInputStream fr;

    public void readAll(){


    try {
        fr = new FileInputStream(new File("books/Artificial intelligence.txt"));


    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        System.out.println("File Not Found");
        e.printStackTrace();
    }
     CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
     decoder.onMalformedInput(CodingErrorAction.IGNORE);

    InputStreamReader reader = new InputStreamReader(fr, decoder);
    BufferedReader br = new BufferedReader(reader);

        try {
            int i = 0;


             for(String newLine; (newLine = br.readLine()) != null; )
                {

                newLine = br.readLine();

                i++;


                System.out.println(newLine);    
            }
            br.close();
            System.out.println(i);


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

To read this txt file it is about 420.000 lines long: Artificial intelligence.txt

But my code above dont read it correctly it is missing about half of the lines in the middle, and seems to start anywhere (each start randomly) the following is one of the possible result of the SYSOut :

Only first lines:

#@Margaret H. Szymanski,Paul M. Aoki,Rebecca E. Grinter,Amy Hurst,James D. Thornton,Allison Woodruff
#cComputer Supported Cooperative Work
#%5488
#%87739
#%257074
#%818174
#!
#*Unpacking Tasks: The Fusion of New Technology with Instructional Work.
#t2008
#index831790
#%174882
#!

So the question is Why?

Printout of i is always 209647.

Upvotes: 2

Views: 635

Answers (4)

displayname
displayname

Reputation: 1

for(String newLine; (newLine = br.readLine()) != null; )
        {

            i++;

            System.out.println(newLine);
        }

Upvotes: 0

jrhee17
jrhee17

Reputation: 1167

You're calling br.readLine() twice

for(String newLine; (newLine = br.readLine()) != null; )
{

    newLine = br.readLine();

    i++;


    System.out.println(newLine);    
}

You can get rid of the one inside the loop

for(String newLine; (newLine = br.readLine()) != null; )
{

    i++;


    System.out.println(newLine);    
}

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79877

You're calling br.readLine() twice in your loop, but only using the result of one of those two calls in your System.out.println call. So you're only printing out every second line.

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44854

Well you are reading the line twice

once in

 for(String newLine; (newLine = br.readLine()) != null; )
            {

and then again in

            newLine = br.readLine();

nicer would be

while ((newLine = br.readLine()) != null) {....}

Upvotes: 4

Related Questions