Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Swing / AWT / SWT and the fly likes Reading files into JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Reading files into JTextArea" Watch "Reading files into JTextArea" New topic

Reading files into JTextArea

Bob Zoloman
Ranch Hand

Joined: Jul 02, 2006
Posts: 72
I'm trying to read multiple text files into a JTextArea. I'm currently selecting them via JFileChooser dialog, which returns the selectedFiles back as a File[]. When I try and output the files contents it only displays the last file read in.

Here's my code:

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
You could write the sequence of files into a temp file and read it into the text area. Or, you could make a PlainDocument, write the files into it with the insertString method and set the document in the text area.
Or, you could use the append method and write as you read the files:
Bob Zoloman
Ranch Hand

Joined: Jul 02, 2006
Posts: 72
For large text file append goes really slow. I think I'll try merging the files into one large file and initializing the JTextArea with that. Thanks for the help.
It is sorta covered in the JavaRanch Style Guide.
subject: Reading files into JTextArea
Similar Threads
How to keep formatting while reading files
this is working but i don't understand why!!
filenotfound Exception encountered,kindly help
Standard idiom for reading text files
problem in rediredtin GUI to IO Streams