• Post Reply Bookmark Topic Watch Topic
  • New Topic

writing to a text file from JTextArea?

 
Jonnathan Castillo
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I'm trying to write to a file through JTextArea. I was hoping there would be a way to open a JTextArea window where you could see the contents of a txt file and then append to it / change it in that same window. Kind of like a text editor. But I haven't really been able to figure out how to do this. The best I've been able to do is:



is there a specific method I should use, or is JTextArea unable to do this? I'm pretty lost on this lol
 
Carey Brown
Bartender
Posts: 2261
28
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTextArea can't do this. JTextArea has setText() and getText() methods for accessing the text. It's up to you to connect those to file reading and writing methods, and perhaps to add an ActionListener to detect when the text in the pane is modified.
 
Campbell Ritchie
Marshal
Posts: 52586
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You have two tasks, which should be separated. One is reading the contents of the text area, which is really simple. I suggest you print the contents of the text area to System.out, just to prove you have captured the text correctly. Then write it to the file with the usual techniques.
I can see several things wrong with what you are doing:-
  • 1: You seem confused by what you are writing to where.
  • 2: You are not definitely closing the writer; this should be done with try‑with‑resources (Java7+).
  • You might do well to divide the text into multiple lines, in which case you might have a List<String> or a String[] array. Remind yourself about try‑with‑resources and about file IO in the Java™ Tutorials.
     
    Rob Spoor
    Sheriff
    Posts: 20822
    68
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:JTextArea can't do this. JTextArea has setText() and getText() methods for accessing the text. It's up to you to connect those to file reading and writing methods, and perhaps to add an ActionListener to detect when the text in the pane is modified.

    JTextArea, like any JTextComponent, is backed by a Document. That's an interface, so it should be possible to write a Document implementation that does what's needed. Alternatively, the Document can have a DocumentListener added to it. A third way is to use a DocumentFilter and attach that to an AbstractDocument. I'd go for the latter myself.

    However, it's going to be tricky to support edits in the middle of the document or deletes without writing the entire document (or large portions of it) for every update.
     
    Rob Camick
    Ranch Hand
    Posts: 2700
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:JTextArea can't do this. JTextArea has setText() and getText() methods for accessing the text. It's up to you to connect those to file reading and writing methods


    It also has read(...) and write(...) methods which allow you to easily load data into the JTextArea from a file and save the data in the JTextArea to a file.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!