Vikas     Feb,08 2017

Read File Line by Line in ColdFusion by Using Java Classes

I was having situation where I wanted to read a file and process each line.
I think cffile tag is not capable to do it, so I use java classes.
Basically we need two kinds of classes.
1. InputSream
2. Buffer reader

Buffer reader is capable to read any input stream line by line.

Final code will look like:

<cfscript>FileName = expandPath('/temp.txt');objFileReader = createObject("java","");InputStreamReader = objFileReader.init(FileName);objBuffer = createObject("java","" );LineIO = objBuffer.init(InputStreamReader);</cfscript><cfset eof = 0><cfset cnt = 1><cfloop condition="not eof"><cfset currline = LineIO.readline()><cfif isdefined("currline") eq "no"><cfset eof = 1><cfbreak></cfif><cfoutput>line #cnt#: #currline#<br/></cfoutput><cfset cnt = cnt + 1><cfflush></cfloop>