Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedReader & BufferedWriter - write(<string>) writes 10 lines while readline() reads only five.  RSS feed

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

My testWriteFile method writes 10 lines in a BuffereredWriter file but when I try to read the file in method testReadFile, I get only five lines. Why is my program reading only five lines? How can I change my code to retrieve all lines from my file?
Thanks for your help.

My Code is as follows.



The output I got is -->

run:
File Exists = true
Mkdir command successful ?false
File exists?true
File exists now?true
Writing to file
Reading from file
Hi. This is Record no 1.
Hi. This is Record no 3.
Hi. This is Record no 5.
Hi. This is Record no 7.
Hi. This is Record no 9.
BUILD SUCCESSFUL (total time: 0 seconds)

When I browse the file, I can see that the testWriteFile() method has written 10 records correctly. I can't understand why readLine() method in testReadMethod() is reading five records only..

Please help...
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the relevant code:



In fact br.readLine() does read each of the 10 lines. It's just that your code is written to ignore half of them.

In the first line, br.readLine() reads line #0. It isn't null so the statement in the second line is executed. It reads line #1 and writes it to the console.

Then in the first line, br.readLine() reads line #2. It isn't null so the statement in the second line is executed. It reads line #3 and... do you see what's going on?

Change your code to save the value returned by br.readLine() in the first line, and just use that value in the second line.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Thanks so much for your response. I've understood now that I'm invoking readLine() twice and this is causing the problem.

I tried to change the code by having readline() inside the loop but in that case how should I code my loop control instruction to read till the end of the file? Is there any other way to test if the end of file has reached?

If I read the first record outside the loop in a string variable as follows.

String tempString = br.readLine();

and then have my loop instruction as follows.

while (tempString != null)
{ System.out.println(tempString);
tempString = br.readLine();
}

I will be creating 10 strings in the strings pool.

Is there a better way to do this?

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I'm also not sure if System.out.println(<string>) also creates 10 strings in the string pool.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!