How can I view a text file in a JTextArea without any user input?
Created May 4, 2012
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.