| Author |
Jtextarea read line by line
|
Mike Phillip
Ranch Hand
Joined: Dec 05, 2007
Posts: 37
|
|
how can I read line by line the content of a Jtextarea? thanks
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
One idea is to split the getText() results by line breaks, using the regular String.split() method.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Brian Cole
Author
Ranch Hand
Joined: Sep 20, 2005
Posts: 852
|
|
Originally posted by Mike Phillip: how can I read line by line the content of a Jtextarea?
It depends on what you mean by "read". The content is already in RAM, so there's not really i/o involved. If you just want to process each line you can do something like this: String[] lines = yourTextArea.getText().split("\\n"); If the content is large and you're worried about essentially doubling its RAM footprint, then you might want to look into yourTextArea.getDocument().getText(offset, length, yourSegment). That doesn't help you go line by line, but it can minimize copying the text.
|
bitguru blog
|
 |
Mike Phillip
Ranch Hand
Joined: Dec 05, 2007
Posts: 37
|
|
thanks on post guys. I just resolved it. I needed to put it in a file, so I did this: String ln = System.getProperty("line.separator"); String text = myJtxa.getText() ; String as = text.replaceAll("\n", ln); buf.write(as.toString(),0, as.length()); buf.close(); works thanks
|
 |
Brian Cole
Author
Ranch Hand
Joined: Sep 20, 2005
Posts: 852
|
|
Originally posted by Mike Phillip: I needed to put it in a file, so I did this: String ln = System.getProperty("line.separator"); String text = myJtxa.getText() ; String as = text.replaceAll("\n", ln); buf.write(as.toString(),0, as.length()); buf.close(); works
It would have been simpler to just call myJtxa.write(...). It even takes care of line.separator for you. I would have mentioned it earlier had you mentioned anything about writing files in your original query.
|
 |
Daniele Silvi
Greenhorn
Joined: Jun 18, 2012
Posts: 1
|
|
Brian Cole wrote:
String[] lines = yourTextArea.getText().split("\\n");
And then I just did
Worked like a charm !!!
|
 |
 |
|
|
subject: Jtextarea read line by line
|
|
|