aspose file tools
The moose likes I/O and Streams and the fly likes Text file to string and vice versa Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "Text file to string and vice versa" Watch "Text file to string and vice versa" New topic
Author

Text file to string and vice versa

Brian C Robinson
Greenhorn

Joined: Feb 21, 2002
Posts: 7

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

Joined: Jan 30, 2000
Posts: 18652
Try getText(). When you look at the API for a class, be sure to also check for inherited methods from base classes.


"I'm not back." - Bill Harding, Twister
Brian C Robinson
Greenhorn

Joined: Feb 21, 2002
Posts: 7
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

Joined: Jan 30, 2000
Posts: 18652
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.
 
 
subject: Text file to string and vice versa
 
Threads others viewed
StringBuffer to File - Need Example
java application using swing
find multiple occurences of string in file
compile time errors, why?
close a FileReader in the finally clause
IntelliJ Java IDE