This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jtextarea read line by line

 
Mike Phillip
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I read line by line the content of a Jtextarea?




thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One idea is to split the getText() results by line breaks, using the regular String.split() method.
 
Brian Cole
Author
Ranch Hand
Posts: 907
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Phillip
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 907
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Cole wrote:

String[] lines = yourTextArea.getText().split("\\n");



And then I just did


Worked like a charm !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic