Shaun Mccran

My digital playground

18
F
E
B
2009

Reading a file using a Java object

Sometimes in Coldfusion development its handy to use coldfusions standard functions such as CFfile or CFdirectory. But you can also invoke the underlying Java objects that share similar functionality. These can often be more efficient too, as there is no coldfusion server compilation layer involved.

For one piece of work I had recently I was reading a series of files, parsing them out and inserting a the collection of records into a database.

So I created used a java file reader object to read the files.

There is the odd extra config line in here, as the server pathing is unix based not windows based, and I'm dynamically reading the files from the directory.

view plain print about
1<cfset variables.dir = cgi.path_translated>
2<cfset variables.replaceVar = listlast(variables.dir, '\')>
3<cfset variables.dir = replaceNoCase(variables.dir, variables.replaceVar,'', 'all')>
4<cfset variables.filename = variables.dir & "datafile#url.fileNo#.txt">

Next I use the file name variable in the java FileReader object call. I wrap the file reader in a BufferedReader object as this greatly reduces the operation overhead of the FileReader object as the data is buffered into memory and not read from the disk.

Next I'll loop over the outptut string from the buffered file reader object, parsing each line and invoking the cfc method of 'processContent', passing in the string (line of data) as the argument 'content'. '

view plain print about
1<cfscript>
2        cnt = 0;
3        srcFile = variables.filename;
4    
5        // create a FileReader object
6
        fr = createObject("java","java.io.FileReader");
7        fr.init(srcFile);
8    
9        // create a BufferedReader object
10
        br = createObject("java","java.io.BufferedReader");
11        br.init(fr);
12        str = br.readLine();
13    
14        // writeOutput(str);
15
        while isDefinedd("str")) {
16            cnt = cnt + 1;
17            str = br.readLine();
18                if(isDefined("str")){
19                    //writeOutput(cnt);
20
                    //writeOutput(str);
21
                    // pass to CF to do the rest
22
                    processContent(str);
23                }
24            }
25        br.close();
26        //writeOutput(cnt);
27
    
</cfscript>

Now you can perform any sort of parsing you want to perform on the content. In the example below I am creating a local structure and populating it with each delimited value. This allows me to use standard notation to pick out individual elements for whatever processing you want.

view plain print about
1<cffunction name="processContent" output="true" hint="function for processing the file content">
2        <cfargument name="content" type="string">
3    
4        <cfset var local = structnew()>
5    
6        <cfset local.today = now()>
7
8        <cfset local.filecontent = replace(arguments.content,'\n',"", "all" )>
9    
10        <!--- list to array based on tab --->
11        <cfset local.data = local.filecontent.split("\t")>
12        <cfset local.variableName1 = local.data[1]>
13        <cfset local.variableName2 = local.data[2]>
14        
15        Etc...
16
17    </cffunction>

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Back to top