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();
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.
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!