aspose file tools*
The moose likes I/O and Streams and the fly likes Need help optimizing output to file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Need help optimizing output to file" Watch "Need help optimizing output to file" New topic
Author

Need help optimizing output to file

James Beeson
Greenhorn

Joined: Apr 18, 2000
Posts: 18
I have a class that takes a resultSet object and parses it out to a StringBuffer, which is then dumped into a String object called 'wholeString'. My code to write the String to file works, but it is slow. I don't know anything about I/O, but I've heard that output to file can be optimized through the use of buffers. Can someone show me how I could modify this code to use buffers or anything else to optimize it.
Thank you very much!
// Create the file.
DataOutputStream myout = new DataOutputStream(new FileOutputStream(new File("output.text")));

// write to the file, and close it.
myout.writeBytes(new String(wholeString));
myout.close();
sai challa
Ranch Hand

Joined: Feb 06, 2001
Posts: 54
You can add a buffer to an output stream to make the output operations more efficient by using the BufferedOutputStream class.As you write data to a buffered stream,the data is accumulated in a buffer in memory and is only written to the ultimate output stream when the output is full.
DataOutputStream myout=new DataOutputStream(new BufferOutputStream(new FileOutputStream(new File("output.txt"))));
myout.writeChars(new String(wholeString));
Hope this helps.
sai challa
Ranch Hand

Joined: Feb 06, 2001
Posts: 54
I am sorry there was a small typo.
It should be:
DataOutputStream myout=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("output.text"))));
James Beeson
Greenhorn

Joined: Apr 18, 2000
Posts: 18
Thank you! That dramatically increased the speed of the file export!
As I said before, I am parsing through a ResultSet and writing the data to a StringBuffer object. Then I turn the StringBuffer object into a String and write the String to file. Would it be faster (and if so how would I) write the data directly so some kind of stream that would be linked directly to the buffered DataOutputStream you described below?
Thanks again for any help on this subject!

Originally posted by sai challa:
I am sorry there was a small typo.
It should be:
DataOutputStream myout=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("output.text"))));
lee dalais
Ranch Hand

Joined: Feb 28, 2001
Posts: 67
have u thought of :
DataOutputStream myout=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("output.text"))));
myout.writeChars(wholeString.toString());
i would have to c how u're parsing the resultSet to the stringBuffer, to work out a direct connection.
Matts Smith
Ranch Hand

Joined: Feb 03, 2001
Posts: 113
Why don't you write the file straight from the resultset data. you'll save 2 operation on the data.
It does not seem really clean to me but sometime a dirty patch is all you can afford.
James Beeson
Greenhorn

Joined: Apr 18, 2000
Posts: 18
That looked like it would work, but the text file keeps being empty. Do I need to tell the myout to flush the buffer to text or something??

Originally posted by lee dalais:
have u thought of :
DataOutputStream myout=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("output.text"))));
myout.writeChars(wholeString.toString());
i would have to c how u're parsing the resultSet to the stringBuffer, to work out a direct connection.

James Beeson
Greenhorn

Joined: Apr 18, 2000
Posts: 18
Oops! I found the problem. Thanks for the help!

Originally posted by lee dalais:
have u thought of :
DataOutputStream myout=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("output.text"))));
myout.writeChars(wholeString.toString());
i would have to c how u're parsing the resultSet to the stringBuffer, to work out a direct connection.

Rob Jones
Greenhorn

Joined: Aug 14, 2003
Posts: 4
Wow. I'm glad I found this thread. My simple test program just increased performance 1000%.
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help optimizing output to file