Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

why bufferedreader behaves like this?

 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i use BufferedReader created by using FileReader object as the parameter to its constructor
like FileReader fr = new FileReader("a.txt");
BufferedReader br = new BufferedReader(fr);
and now i read contents like this way
StringBuffer sb = new StringBuffer();

while(br.read()!=-1)
{
sb.append(br.readLine());
}
Now suppose my contents
of a.txt are like this way
hello world
I m fine
how are you
then contents of stringbuffer made come out to be
ello world
I m fine
ow are you
That means it skips characters at position 1
then how can i retrieve the full contents of a.txt in my stringbuffer
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
your br.read reads the first byte of each line

it's not tested but it should work.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
Even I tried & found the same results, can anyone explain Y?

Originally posted by Gaurav Chikara:
If i use BufferedReader created by using FileReader object as the parameter to its constructor
like FileReader fr = new FileReader("a.txt");
BufferedReader br = new BufferedReader(fr);
and now i read contents like this way
StringBuffer sb = new StringBuffer();

while(br.read()!=-1)
{
sb.append(br.readLine());
}
Now suppose my contents
of a.txt are like this way
hello world
I m fine
how are you
then contents of stringbuffer made come out to be
ello world
I m fine
ow are you
That means it skips characters at position 1
then how can i retrieve the full contents of a.txt in my stringbuffer



------------------
ANaveenS
 
Naveen Arumugam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
U can try this way it works.
Explanation: I think whenever u read a byte is read & one position is skipped after that u r making a call to readLine().{i'm not sure with this}
Also ,U can use this way or U can just check for null using readLine() in while loop
import java.io.*;
class AAA1
{
public static void main(String args[])throws Exception
{
FileReader fr = new FileReader("a.txt");
BufferedReader br = new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
int c;
while((c = br.read()) !=-1)
{
// code changed here
sb.append((char)c);//br.readLine());
}
System.out.println(sb);
}
}
 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic