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 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 Blogger J&D, at 9/18/2009 10:06:00 pm  

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

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



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

潤滑液,SM,內衣,性感內衣,自慰器,充氣娃娃,AV,
情趣,G點,性感丁字褲,情趣,角色扮演服,吊帶襪,丁字褲,情趣用品,無線跳蛋,男女,


情趣按摩棒,自慰套,角色扮演,按摩棒,跳蛋,情趣跳蛋,
.,
按摩棒,電動按摩棒,飛機杯,視訊,自慰套,自慰套,情趣用品,情趣內衣,



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  

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



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

Trackbacks:

» Create a Link