aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Jtextarea read line by line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Jtextarea read line by line" Watch "Jtextarea read line by line" New topic
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: 19543
    
  16

One idea is to split the getText() results by line breaks, using the regular String.split() method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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: 862
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 !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jtextarea read line by line
 
Similar Threads
JTextArea
Weird chars at end of line in JTextArea
Display problem in JFrame
JTextArea realtime update
Which Swing Component to Use to view log file ??