• Post Reply Bookmark Topic Watch Topic
  • New Topic

read text file into text area  RSS feed

 
Robert Oliver
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • 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 ]
 
Mark Nicholas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Mark. Worked great. Simple and straight forward.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!