Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sultan Altoobi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4369
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Cole Terry
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20954
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 2559
    9
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks alot to all of you.
    great comments and thank you all again.
    I will make changes.

    My Regards,,,
     
    Sultan Altoobi
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 5125
    11
    Java Netbeans IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Sultan Altoobi
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry! It not allow me to attach it.

    code here it is:

     
    Tim Moores
    Bartender
    Posts: 2730
    36
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic