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

how to clear/reset buffer when I use BufferedReader?  RSS feed

 
Serena Chen
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi! in my program, I use BufferedReader to get a string at a time from another program using socket.
anyway, I have BufferedReader in and I call in.readLine() inside the while loop. It's not starting to read from the beginning each time.
ie.
inside while loop
1)a string "abc" shows up, in.readLine() reads "abc"
2)the second string "def" shows up, in.readLine() is reading "abcdef"! but i want "def" alone.
so I am thinking to reset or clear the buffer everytime but how? I know there is a clear() function in class Buffer but since I am using BufferedReader, how should I do it? I tried to use reset() but it doesn't work.
please suggest some solutions. thanks a lot!!
 
Lewin Chan
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serena,
readLine() from the javadocs

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Your string abcdef isn't separated by any line terminators, so readLine() isn't really under any obligation to what you expect it to...
Exactly how are you sending the data to the BufferedReader?
L
 
Serena Chen
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Lewis, my problem is solved!
btw, you are right, after I added '\n' to every string send over, it is working fine now.
thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!