Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading files into JTextArea

 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic