• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

writing to a text file from JTextArea?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Sheriff
    Posts: 22781
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Rancher
    Posts: 3324
    32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic