How can I view a text file in a JTextArea without any user input?

John Zukowski

Assuming you know what text file to read already, just read the file into a StringWriter. Once you have everything read, get the String from the StringWriter with toString() and call setText() on the JTextArea with the contents. Something like the following code:

FileInputStream input = new FileInputStream(...);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringWriter writer = new StringWriter();
PrinterWriter out = new PrintWriter(writer);
String line;
while ((line = reader.readLine()) != null)  {
  out.println(line);
}
out.flush();
textArea.setText(writer.toString());

In the case of an applet, the FileInputStream line would be something like a getInputStream() from a URLConnection.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact