Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Text file to string and vice versa  RSS feed

 
Brian C Robinson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is what I have. I'm wondering if there's a better solution. The text_ variable is a JTextArea member of the class.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try getText(). When you look at the API for a class, be sure to also check for inherited methods from base classes.
 
Brian C Robinson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how getText() will help. I guess I should have been clearer about the help I wanted. I don't like the character by character read in the load() method. What is an alternative to that.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops - I didn't read carefully enough. You can user the read(char[]) and write(char[]) to handle a large number of characters at once. You'll need to create your own char[] array as a buffer, transferring chars from one stream to another. That's generally the preferred way to make IO more efficient. It will still need to be in a loop, as you can never guarantee how many chars will be read each time - but you can do a lot more reading/writing in each iteration.
Also, you may find it useful to presize your StringBuffer using the length() method of the File class. This also depends on the character encoding used. It speeds things up if the StringBuffer doesn't have to keep resizing itself.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!