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

read text file into text area

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to figure out how to read a text file and read it into a text area in my program. This should be fairly straight forward but I can't seem to get anything to work.
This doesn't work and I have tried several variations.


File filename = jfc.getSelectedFile();
try{// opens try
FileReader in = new FileReader(filename);
BufferedReader br =
new BufferedReader(in);
int c;
String s = br.read();
while ((c = in.read()) != -1)
br.write(c);
comments.append("\n" + s +"\n");
in.close();
br.close();
}// closes try

 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you're doing here, but surely "br.write(c)" is wrong if br is a BufferedReader. The compiler should complain.
[ August 20, 2002: Message edited by: Ron Newman ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enjoy, pass a string as the filepath, then get the contents of the file via getFileContent() which returns a stringbuffer. To create a string call
the toString() of the stringbuffer object. This code is really fast !!

[ August 20, 2002: Message edited by: Mark Nicholas ]
[ Edited by Dave to format code ]
[ August 20, 2002: Message edited by: David O'Meara ]
 
Robert Oliver
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark. Worked great. Simple and straight forward.
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant 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