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

problem while reading from file  RSS feed

 
gaurav chhabra
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir i have following code


FileReader fr=new FileReader("dheeraj_chat.txt");
int ch;
StringBuffer st=new StringBuffer();
while((ch=fr.read())>0)
st=st.append((char)ch);
System.out.println("string new= "+st.toString());


this code i readingonly first line of the file .....not the rest of file...

plz tell me what to do

thank you
gaurav
 
Annie Smith
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it necessary to read character by character? You could try using a BufferedReader.




[ March 31, 2005: Message edited by: Annie Smith ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by gaurav chhabra:
While unlikely, if you have a \x00 (null character, not a literal '0' character) in your file, your loop will terminate early. You should change the loop condition toOf course, Annie's suggestion of using readLine() from BufferedReader will be easier and perform better.
[ March 31, 2005: Message edited by: David Harkness ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!