Results 1 to 12 of 12
  1. #1
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Default Question about a recent thread: reading from a JTextComponent

    In a recent thread where i've partecipated too..

    the original poster wanted to write in a file the content of a jtextarea.. when i said that you can use the write() method of JTextComponent, or if you prefer use getText() and manually write the string into the file,...

    camickr said:

    "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. "

    That's absolutely correct!... but.. if i need to process/modify/doSomething to the string in the jTextArea BEFORE to write the string in a file... i can't use
    Java Code:
    jTextArea.write(myWriter);
    So in that situation what should i do? ..in order to avoid the problem that camickr said?

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

    Default

    Why exactly can't you use
    Java Code:
    jTextArea.write(myWriter);
    ?

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    As far as I understand, he doesn't want to write the String that's returned by getText(), but a modifyed String.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    Okey, well just use a PrintWriter then and use PrintWriter.println() for the new lines?

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

    Default

    Quote Originally Posted by PhHein View Post
    As far as I understand, he doesn't want to write the String that's returned by getText(), but a modifyed String.
    that's the point! But if i use getText() and i write the returned String in a file using for example a FileWriter i'll find the problem that camickr said.

    Answering to r035... the only way (that i can think) to use the write method is:
    Java Code:
    String s = jtext.getText();
    
    //doSomething whith s
    
    jtext.setText(s);
    
    jtext.write(myWriter);
    but doing things in this way force the user watching the GUI, to see the String modified in JTextArea..... isn't there any smarter solution you can suggest me in order to avoid the call "setText()"?

    thanks

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

    Default

    Quote Originally Posted by r035198x View Post
    Okey, well just use a PrintWriter then and use PrintWriter.println() for the new lines?
    i start answering before to read your post...

    thanks for your answer, so what camickr said in previous thread was not totally true... we can manually write the String from getText() but using PrintWriter..

    thanks again... now everything is clear.

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

    Default

    Quote Originally Posted by r035198x
    ... well just use a PrintWriter then and use PrintWriter.println() for the new lines

    This message is no longer a very short message therefore the forum software will now allow it to be submitted.

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

    Default

    Quote Originally Posted by atom86 View Post
    ... so what camickr said in previous thread was not totally true... we can manually write the String from getText() but using PrintWriter...
    I would be very careful with such statements if I were you.

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

    Default

    Quote Originally Posted by r035198x View Post
    This message is no longer a very short message therefore the forum software will now allow it to be submitted.
    sorry i don't understand what do you mean..

    Quote Originally Posted by r035198x View Post
    I would be very careful with such statements if I were you.
    HEY! CALM down friend!! This is a java forum.. not a mafious family where if a boss say "2+2=5" we must say "yes it's true"..
    personally i thanks all of you for your help but i don't care who you are.... camickr is a person just like me and you he's not a god..maybe an excellent java programmer but he's not a god... i'm here to improve my java-programming skills and understand what i can and what i can't do with java... any other reason...

    so YOU said that with PrintWriter YOU can write correctly newlines... "bypassing" the problem that camickr said previously... is it correct?

  10. #10
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Uh oh. :eek:
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  11. #11
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    See the discussion of line ending in DefaultEditorKit. The internal line ending is always '\n'

    The application may call for a specific form of line terminator, say RS ('\u00E3').
    If so the code could be:
    Java Code:
    String s = jtext.getText();
    // do Something with s
    s = s.replace('\n', '\u00E3');
    stream.write(s);
    or one could manually convert to the system dependent line terminator
    Java Code:
    String s = jtext.getText();
    // do Something with s
    s = s.replace('\n', System.getProperty("line.separator"));
    stream.write(s);

  12. #12
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Well the point of my comment was that you can't just write out the entire string when you use getText(). If you use getText() then you would need to alter the String by replacing the "\n" using the suggestions above. Then you can write the text out using any means you want. It would not have to be a PrintWriter.

    However, just using a PrintWriter will not solve your problem because you can't just use getText() and then use the PrintWriter to write it out. The newlines characters imbedded in the string will not automatically be converted. Instead you need to read the text one line at a time from the text area, then you need to write that line of text, then you need to manually add a new line. So you have to write your own loop to process each line in the text area individually.

Similar Threads

  1. [SOLVED] File reading question
    By wiz0r in forum New To Java
    Replies: 5
    Last Post: 04-19-2009, 01:21 AM
  2. DirectoryDialog - Recent folder
    By shyamchander in forum SWT / JFace
    Replies: 1
    Last Post: 02-23-2009, 12:35 PM
  3. Thread question
    By Moncleared in forum Advanced Java
    Replies: 5
    Last Post: 02-09-2009, 10:33 PM
  4. Where to find most recent java platform???
    By busdude in forum New To Java
    Replies: 5
    Last Post: 11-16-2008, 03:19 AM
  5. Question abt.reading xml file using java
    By gvi in forum Advanced Java
    Replies: 6
    Last Post: 11-08-2007, 05:48 PM

Posting Permissions

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