My Projects
Search Blog

Categories
Archives
Photo Albums
RSS

Powered by
BlogCFM v1.15

12 July 2006
Reading large files with java versus CFFILE

A question was posted on the cf-talk list (thread) about reading large files with CFFILE and problems they were having.

I suggested trying java to read the large file line by line and I posted the following code:

<cfsetting showdebugoutput="Yes">
<cfscript>
 cnt = 0;
 // large text file, 4MB, 80,000+ lines
 srcFile = "E:\Inetpub\wwwroot\tools\mass_email\list.dat";
 // create a FileReader object
 fr = createObject("java","java.io.FileReader");
 // Call the constructure with the source file path
 fr.init(srcFile);
 // create a BufferedReader object
 br = createObject("java","java.io.BufferedReader");
 // call the constructor with the FileReader as the arg
 br.init(fr);
 // read the first line
 str = br.readLine();
 // loop ... str will be undefined if there are no more lines
 while (isDefined("str")) {
  // do stuff with the string
  cnt = cnt + 1;
  // read the next line so we can continue the loop
  str = br.readLine();
 }
 // close the buffered reader object
 br.close();
 writeOutput(cnt);
</cfscript>

 

The code above was tested on CFMX 7 and it does work.  On my server, it consistently returns the results in about 400ms (ranging between 350ms and 500ms).

In order to compare, I wrote some CFML code that does essentially the same thing using CFFILE and looping through the file content as a list with chr(10) as the delimiter.

The CFFILE route was slower and much more erratic, ranging from 450ms to over 2000ms - probably averaging 1400ms in the 20-30 times I reloaded the page.

So if you're reading a large file and doing line by line processing - consider using native java rather than CFFILE.

Posted by rickroot at 2:15 PM | Link | 1 comment
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: Reading large files with java versus CFFILE
I'm using ajax and would like to open a filereader, read a line each time the fielreading .cfm is called and close the filereader after the entire file has been read. Is there any way to do this (i.e. kep the filereader open until i say to close it)? As far as I can tell, I have to Re-create the filereader each time I call the .cfm that does the reading.
Posted by xlisax on July 19, 2006 at 10:20 AM

Post a comment (login required)