Please note, new blog at http://www.acheron.org/darryl/

Using Java BufferedWriter to create CSV files

Someone recently asked me to expand upon my previous post about generating CSV files, and in particular, to focus on the Java method. In my previous post, I concluded that using the Java BufferedWriter class is a much more scalable and stable solution than using the typical concatenate-and-write-once-done method.

Step by Step

There really isn't much to using Java to write to a file. Essentially, there are three steps, 1) create the Java class instances, 2) get the data and write to the buffer, and 3) close the output stream.

Firstly, we instantiate the FileWriter class (which writes to the file) and then the BufferedWriter class, passing the FileWriter instance as a parameter.

CFM:
  1. <cfscript>
  2. outputFile = getDirectoryFromPath(GetCurrentTemplatePath()) & "dump.csv";
  3. oFileWriter = CreateObject("java","java.io.FileWriter").init(outputFile,JavaCast("boolean","true"));
  4. oBufferedWriter = CreateObject("java","java.io.BufferedWriter").init(oFileWriter);
  5. </cfscript>

Then, we create the column headers, and loop over the data (query in this case) and write it to the buffer. The Java class manages the size of the buffer internally, so when threshold is reached, it will write to the file.

CFM:
  1. <cfset oBufferedWriter.write("LASTNAME,FIRSTNAME" & chr(13) & chr(10))>
  2. <cfloop query="qData">
  3.    <cfset oBufferedWriter.write(chr(34) & lastname & chr(34) & ",")>
  4.    <cfset oBufferedWriter.write(chr(34) & firstname & chr(34) & chr(13) & chr(10))>
  5. </cfloop>

Lastly, we need to close the output stream, otherwise the file will not be released.

CFM:
  1. <cfset oBufferedWriter.close()>

Full code

CFM:
  1. <cfscript>
  2.    outputFile = getDirectoryFromPath(GetCurrentTemplatePath()) & "dump.csv";
  3.    oFileWriter = CreateObject("java","java.io.FileWriter").init(outputFile,JavaCast("boolean","true"));
  4.    oBufferedWriter = CreateObject("java","java.io.BufferedWriter").init(oFileWriter);
  5. </cfscript>
  6.  
  7. <cfquery datasource="cfartgallery" name="qData">
  8. SELECT *
  9. FROM artists
  10. ORDER BY lastname, firstname
  11. </cfquery>
  12.  
  13. <cftimer type="inline" label="Generate CSV">
  14.    <cfset oBufferedWriter.write("LASTNAME,FIRSTNAME" & chr(13) & chr(10))>
  15.  
  16.    <cfloop query="qData">
  17.  
  18.       <cfset oBufferedWriter.write(chr(34) & lastname & chr(34) & ",")>
  19.       <cfset oBufferedWriter.write(chr(34) & firstname & chr(34) & chr(13) & chr(10))>
  20.  
  21.    </cfloop>
  22.    <cfset oBufferedWriter.close()>
  23. </cftimer>

By Anonymous Anonymous, at 2/19/2006 09:30:00 pm  

This is a great post. Once you start getting into large file sizes(50MB+), using buffered readers and writters are a necessity in ColdFusion MX. JRun instances are usually set anywhere from 100MB to 512MB. ColdFusion MX Standalone can be set to around 1500MB. You can max out your JRun instance with ColdFusion by tring to use CFFILE on large files.



By Anonymous Anonymous, at 8/26/2006 06:49:00 pm  

That was a very informative blog. But what maybe I am worried about is I can't relate to it. I updated surely of the latest in the tech world. But not as much as you are people.



By Anonymous Anonymous, at 8/29/2006 06:01:00 pm  

Well, you really did a great job on epounding your topic. At least it was easy to understand. Thus, it was eplained in a very simple and effective way.



By Anonymous Anonymous, at 9/18/2009 10:06:00 pm  

av女優
av情人趣味用品愛爾蘭情人趣味愛蜜莉情人趣味用品情趣用品

情趣用品情趣用品aio交友愛情館一葉情貼圖片區一葉晴貼影片區



By Blogger acekard, at 9/08/2011 04:38:00 pm  

r4ds
r4i
r4i gold
r4i sdhc 1.4.2
acekard 2i 3ds
ttds
m3 real
supercard 3ds
cyclods iEvolution
ps3 move gun
ps3 Wireless controller
r4i 3ds
ds card
acekard 3ds
dstt 3ds
dstwo
nintendo r4
r4 nintendo ds
r4i card



By Blogger sabreena, at 3/05/2012 06:05:00 pm  

Nice post.and it is Amazing post as well . :)



----------------

Online casino



By Blogger alina, at 3/12/2012 04:17:00 pm  

A like it very much. Thanks



-----------

Online slots



» Post a Comment