• 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

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

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

  •  
    Rancher
    Posts: 3324
    32
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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!!!
     
    Bartender
    Posts: 5167
    11
    Netbeans IDE Opera Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry! It not allow me to attach it.

    code here it is:

     
    Saloon Keeper
    Posts: 7585
    176
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic