Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help optimizing output to file

 
James Beeson
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. I'm glad I found this thread. My simple test program just increased performance 1000%.
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic