You are here:
Adobe Apollo Adobe Apollo

cffile crashes out – try using java.io.BufferedWriter?

A few months back I was confronted with the problem of writing a fairly large CSV file using ColdFusion. The file size was around 10mb so I got to work with cffile. After building the code, I sat back and pushed the button to test. BANG! the memory on the ColdFusion server was sky high and the file was taking forever to be written to disk (sometimes even timing out or failing due to memory issues).

So I found myself with a problem and needed something that will write data to files in a controlled manner. After some serious googling time I found a post by Daryl Lyons regarding this problem. Using the underlying Java layer of ColdFusion, he creates a BufferedWriter and FileWriter object to do the work for him. So with a little help from Daryl’s post, I began to build a Java version of my file writing template.

The code is a little like this ...

<cfscript>
outputFile = getDirectoryFromPath(GetCurrentTemplatePath()) & “mytest.csv”;
oFileWriter = CreateObject(”java”,”java.io.FileWriter”).init(outputFile,JavaCast(”boolean”,”true”));
oBufferedWriter = CreateObject(”java”,”java.io.BufferedWriter”).init(oFileWriter);
</cfscript>

then loop over your query or required data and write the file, line by line ...

<cfset oBufferedWriter.write(”COL1,COL2″ & chr(13) & chr(10))>
<cfloop query=”qData”>
<cfset oBufferedWriter.write(chr(34) & lastname & chr(34) & “,”)>
<cfset oBufferedWriter.write(chr(34) & firstname & chr(34) & chr(13) & chr(10))>
</cfloop>

Then as in any good programming language, we make sure we clean up our objects for garbage collection

<cfset oBufferedWriter.close()>

So the moment of truth. Would this method actually work … YES it did and it was really quick. The file took seconds to write instead of minutes or even failing. This is a really useful way of writing large files (xml,csv,txt,xls) and would recommend this to anyone with a similar issue. Once again thanks to Daryl Lyons for this excellent example!