• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with buffered reader  RSS feed

 
Suman Mummaneni
Ranch Hand
Posts: 96
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am using the follwing code to read from a .txt file

while(bReader.read()!=-1) {

textData.append("\n"+bReader.readLine());
}
return textData;

When i print the textData file whichi is a string on the console the out put is

"his is test document for the open XP editor project.......
f you see this message then the IO is working other wise you have to do kick ass codeing for thatbest
heers
yy"

As you can see the first letter is not being printed I can not understant why this is happenind for each line only the first letter is not being printed

if any one can help me please reply asap.
 
sasi kala
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi suman,

you are moving the file pointer to one char ahead for each line
in while() condition. instead of that use the below code



Hope This Helps.
All The Best
 
Suman Mummaneni
Ranch Hand
Posts: 96
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sasiKala,

Thanks for the reply. Do you how to control the buffer reader pointer. Is there any method that could return the current position of the buffer reader so that we can move back and forht as we do in c and c++?


 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BufferedReader is one-way. If you want to move back and forth, use java.io.RandomAccessFile.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Suman Mummaneni:
Do you how to control the buffer reader pointer. Is there any method that could return the current position of the buffer reader so that we can move back and forht as we do in c and c++?
Streams and Readers are designed to be one-way as Joe stated, however both do support marking your position and returning to it. The interfaces define the markSupported() method to determine whether the actual class supports this feature. BufferedReader does, and others may as well.

To use it, you set the mark, do some reading, then reset back to the mark. I would definitely not use it in this case but instead use the loop Sasi showed you, but here's how it works:I'll leave it up to you to figure out how to put that into your while loop. Also, I could have sworn one of the InputStream subclasses supported putBack(char c) but can't find it in a quick glance at java.io.
 
Suman Mummaneni
Ranch Hand
Posts: 96
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

The code was really helpful. Thankyou
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're very welcome. Glad you could join us at JavaRanch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!