• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jtextarea read line by line

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can I read line by line the content of a Jtextarea?




thanks
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One idea is to split the getText() results by line breaks, using the regular String.split() method.
 
Author
Posts: 986
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Cole wrote:

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



And then I just did


Worked like a charm !!!
reply
    Bookmark Topic Watch Topic
  • New Topic