aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to Save from JTextArea using JFileChooser as a TXT file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to Save from JTextArea using JFileChooser as a TXT file." Watch "How to Save from JTextArea using JFileChooser as a TXT file." New topic
Author

How to Save from JTextArea using JFileChooser as a TXT file.

Sultan Altoobi
Ranch Hand

Joined: Dec 11, 2011
Posts: 34
Hello Every Body,,

I want to write a code that saves text from JTextArea using JFileChooser as a TXT file.
I did write the following code and it works fine, but the only problem is--> the file will be
saved in the same dierectory of the code, not at the place I choose!!!

any help please!!



any help please!!
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

dont trust me but it looks like the only thing you do different from my code is the order in which you do things.
my File file = saver.getSelectedFile(); is inside the try block . i dont see how it matters but...

actually that might be it though. you are not waiting for the approve



SCJP
Visit my download page
Cole Terry
Ranch Hand

Joined: Nov 23, 2011
Posts: 45
Sultan Altoobi wrote:Hello Every Body,,

I want to write a code that saves text from JTextArea using JFileChooser as a TXT file.
I did write the following code and it works fine, but the only problem is--> the file will be
saved in the same dierectory of the code, not at the place I choose!!!



Well, the solution is very simple: Get the absolute path of the selected file.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18152
    
    8

Cole Terry wrote:Well, the solution is very simple: Get the absolute path of the selected file.


Exactly.

To be specific, the problem is here:

Which throws away the path of the file and uses only the name. Frankly I would replace that code by this:

That has two advantages:
  • It uses the full path of the selected file
  • It doesn't add an extra ".txt" to the end of the name the user chose

  • Rob Camick
    Ranch Hand

    Joined: Jun 13, 2009
    Posts: 2094
        
        7
    Also, to write data from a text component just use:



    instead of getting the text and doing the write yourself. Don't reinvent the wheel.
    Sultan Altoobi
    Ranch Hand

    Joined: Dec 11, 2011
    Posts: 34
    Thanks alot to all of you.
    great comments and thank you all again.
    I will make changes.

    My Regards,,,
    Sultan Altoobi
    Ranch Hand

    Joined: Dec 11, 2011
    Posts: 34
    Hi again,,

    I faced another problem--> When I save as txt file the text from
    JTextArea will be saved in one line only!!!

    for example:

    Hello World.
    This is java.
    End.

    this text from JTextArea will be saved in txt file as follow:

    Hello World.This is java.End.

    I want it to be line by line(separate lines).

    (note: when I save it as word doc, it will be saved line by line without any problem!)

    any help please!!!
    Darryl Burke
    Bartender

    Joined: May 03, 2008
    Posts: 4523
        
        5

    Post your code, in the form of an SSCCE.

    edit: Leave out any JFileChooser code. Just hardcode a file path for saving to -- we are concerned with the code that actually writes out the content, not with the code for selecting a file.


    luck, db
    There are no new questions, but there may be new answers.
    Sultan Altoobi
    Ranch Hand

    Joined: Dec 11, 2011
    Posts: 34
    Darryl Burke wrote:Post your code, in the form of an SSCCE.

    edit: Leave out any JFileChooser code. Just hardcode a file path for saving to -- we are concerned with the code that actually writes out the content, not with the code for selecting a file.


    I need JFileChooser here!!!

    complete code is in attachments.
    Sultan Altoobi
    Ranch Hand

    Joined: Dec 11, 2011
    Posts: 34
    Sorry! It not allow me to attach it.

    code here it is:

    Tim Moores
    Rancher

    Joined: Sep 21, 2011
    Posts: 2408
    If memory serves, JTextArea uses only newlines ("\n"), not both newlines and carriage returns ("\r"), for line endings (which is what Windows generally uses). If whatever software you're using to view the file does not consider newlines without carriage returns as line endings, then what you describe may happen. Especially Windows software is prone to do this. See if it has a setting somewhere for changing this. Alternatively, replace "\n" by "\r\n" in the entire text.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to Save from JTextArea using JFileChooser as a TXT file.
     
    Similar Threads
    Unable to save a file using JFileChooser
    what if I want to write to chooser file rather message the message2=...?
    How to keep formatting while reading files
    saving a text file using JFileChooser
    GUI: Cut/Copy/Paste/Etc.