Results 1 to 11 of 11
  1. #1
    kostinio is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default How we can copy the component of a jtextarea into a file??

    Hello all,
    I would like to ask how i could copy the component of jtextarea into a file as a txt.file?
    In my project i ask the user to write something in a jtextarea and i want the component of the jtextarea that user has typed to be copied into a file..
    Does anyone have any idea?

    Thanks in advance!!! :)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Take a look at the JTextArea methods inherited from JTextComponent, you may find something useful.

    Hint: You'll be looking for a method that takes a Writer as a parameter.

    db

  3. #3
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Default

    javax.​swing.​text.​JTextComponent.write()

    if you don't like this one, you can use "getText()" and manually write the String return value into a file.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Quote Originally Posted by atom86 View Post
    you can use "getText()" and manually write the String return value into a file.
    Please stick with the method suggested by Darryl. Doing the work your self means you won't have the proper "newline" string added to your file depending on the OS you are using.

  5. #5
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Please stick with the method suggested by Darryl. Doing the work your self means you won't have the proper "newline" string added to your file depending on the OS you are using.
    i've never tested this option with the newline in text.. and it's true.. i've tested it now.. if i write into a file the newline char '\n' it become something unreadable.. so i withdraw what i said in the second part of my post... use the write() method.

  6. #6
    kostinio is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    I tried to use this block of code but it doesn't work :S

    Writer output = null;
    File file = new File("out.txt");
    output = new BufferedWriter(new FileWriter(file));
    output.write("message"+getComponent(JTextArea1));
    output.close();
    } catch (IOException ex) {
    Logger.getLogger(ptx.class.getName()).log(Level.SE VERE, null, ex);
    }

    Does anyone have any idea? :)

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You did not read the first response in this thread?

  8. #8
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    You did not read the first response in this thread?
    he did.. but i think he confused the write() method of BufferedWriter with the write() method inerithed by the JTextComponent class (the one suggested by Darryl).

    you should call something like:

    Java Code:
    myjTextArea.write( myWriter );
    where myWriter refeer to the file you want to write.

    This method stores the contents of the model into the given stream. By default this will store the model as plain text (javadocs).

  9. #9
    kostinio is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    Thanks all for your response. :)

    It does work but i want to ask something more.. In my project i want 2 methods to write in this file i call out.txt but when the second method is being called then everything that the first method wrote is erased.. I tried to use the special character \n but it didn't work :S

    any ideas?
    :)

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Read the API specs for FileWriter. There is a constructor which takes a boolean to indicate whether to append the data or not.

  11. #11
    kostinio is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    Thanks again guys i found the reason why this happened.. :)

Similar Threads

  1. Copy a file to a folder.
    By leric in forum New To Java
    Replies: 7
    Last Post: 07-29-2009, 05:11 AM
  2. Replies: 2
    Last Post: 10-05-2008, 06:13 PM
  3. How to revoke permission to save or copy a PDF file in java
    By priyanka.dandekar in forum Advanced Java
    Replies: 11
    Last Post: 10-05-2008, 06:06 PM
  4. java file copy
    By hknyo in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 04:42 AM
  5. copy image/imageicon into a file on disk
    By archanajathan in forum Advanced Java
    Replies: 2
    Last Post: 11-22-2007, 06:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •