Results 1 to 12 of 12
- 11-03-2009, 09:54 AM #1
Member
- Join Date
- Sep 2009
- Location
- Italy, Turin
- Posts
- 39
- Rep Power
- 0
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
So in that situation what should i do? ..in order to avoid the problem that camickr said?Java Code:jTextArea.write(myWriter);
- 11-03-2009, 02:08 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Why exactly can't you use
?Java Code:jTextArea.write(myWriter);
- 11-03-2009, 02:10 PM #3
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
- 11-03-2009, 02:23 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Okey, well just use a PrintWriter then and use PrintWriter.println() for the new lines?
- 11-03-2009, 02:32 PM #5
Member
- Join Date
- Sep 2009
- Location
- Italy, Turin
- Posts
- 39
- Rep Power
- 0
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:
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()"?Java Code:String s = jtext.getText(); //doSomething whith s jtext.setText(s); jtext.write(myWriter);
thanks
- 11-03-2009, 02:39 PM #6
Member
- Join Date
- Sep 2009
- Location
- Italy, Turin
- Posts
- 39
- Rep Power
- 0
- 11-03-2009, 02:41 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Originally Posted by r035198x
This message is no longer a very short message therefore the forum software will now allow it to be submitted.
- 11-03-2009, 02:43 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 11-03-2009, 03:05 PM #9
Member
- Join Date
- Sep 2009
- Location
- Italy, Turin
- Posts
- 39
- Rep Power
- 0
sorry i don't understand what do you mean..
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?
- 11-03-2009, 03:15 PM #10
- 11-03-2009, 04:01 PM #11
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
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:
or one could manually convert to the system dependent line terminatorJava Code:String s = jtext.getText(); // do Something with s s = s.replace('\n', '\u00E3'); stream.write(s);
Java Code:String s = jtext.getText(); // do Something with s s = s.replace('\n', System.getProperty("line.separator")); stream.write(s);
- 11-03-2009, 05:32 PM #12
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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
-
[SOLVED] File reading question
By wiz0r in forum New To JavaReplies: 5Last Post: 04-19-2009, 01:21 AM -
DirectoryDialog - Recent folder
By shyamchander in forum SWT / JFaceReplies: 1Last Post: 02-23-2009, 12:35 PM -
Thread question
By Moncleared in forum Advanced JavaReplies: 5Last Post: 02-09-2009, 10:33 PM -
Where to find most recent java platform???
By busdude in forum New To JavaReplies: 5Last Post: 11-16-2008, 03:19 AM -
Question abt.reading xml file using java
By gvi in forum Advanced JavaReplies: 6Last Post: 11-08-2007, 05:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks