Please note, new blog at

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.

  1. <cfscript>
  2. outputFile = getDirectoryFromPath(GetCurrentTemplatePath()) & "dump.csv";
  3. oFileWriter = CreateObject("java","").init(outputFile,JavaCast("boolean","true"));
  4. oBufferedWriter = CreateObject("java","").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.

  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.

  1. <cfset oBufferedWriter.close()>

Full code

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

By Anonymous David Fekke, 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 tin99, 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 ced155, 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  



By Blogger eda, at 10/03/2009 02:41:00 pm  



By Blogger longge, at 6/25/2010 01:09:00 pm  

These louis vuitton handbags
are continued abiding. You will accept to absorb your money already for purchasing these lv
. You can accumulate on abacus added louis vuitton
, accouterment you added appearance and fashionably advised Louis vuitton bags

By Blogger 太子, at 6/29/2010 12:28:00 pm  

Just like in artist clothing, data can accomplish or breach a hermes bags
. Authenticity cards aren’t the alone authentication of Hermes Kelly
. Abounding appear with booklets to the little data that analyze the Hermes hand bag

If you are absolutely searching to buy Chanel handbag
, you'll be drooling over the advanced array of styles and colors of Chanel
. One of the chanel watches
is its checky covering design.

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

r4i gold
r4i sdhc 1.4.2
acekard 2i 3ds
m3 real
supercard 3ds
cyclods iEvolution
ps3 move gun
ps3 Wireless controller
r4i 3ds
ds card
acekard 3ds
dstt 3ds
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

By Blogger Riya Jacob, at 2/23/2016 11:34:00 pm  

Such a nice post ! great love to read this one.

mumbai escorts
mumbai escorts
mumbai escorts
mumbai escorts

» Post a Comment


» Create a Link