Using CFBufferedReader in ColdFusion

by @jehiah on 2004-01-08 17:45UTC
Filed under: All , ColdFusion

Following is an example of how to read in large files (several Megs) into ColdFusion and parse them as needed.

CFBufferedReader works exactly like, but adds an isEOF() method to indicate whether the last call to readLine detected the end of the file.

To use CFBufferedReader place the .class file in your ColdFusion classpath (and restart). The original code is from Daryl Banttari (Macromedia), but does not look actively developed, so I am reposting it here and will be happy to make any suggested modifications.

Download the Files


<cfset datafile = "c:pathtodatafile.txt">
<cfobject type="Java" class="" name="fr" action="create">
<cfobject type="Java" class="CFBufferedReader" name="reader" action="create">
<!--- read file --->
<cfset fn = cgi.cftemplatepath>
<!--- create a Java FileReader object to read this file --->
<!--- note that is a native Java class.--->
<!--- when calling Java from CF, the constructor is called "init()" --->
<cfset fr.init(datafile)>
<!--- now pass the FileReader object to our extended BufferedReader --->
<cfset reader.init(fr)>
<!--- read the first line from the file --->
<cfset curLine=reader.readLine()>

<!--- now loop until we reach the end of the file --->
<cfloop condition="not #reader.isEOF()#">
    <!--- the variable curLine contains the contents of the current datafile line --->
    <cfoutput>#curLine# <br></cfoutput>
    <!--- grab the next line from datafile --->
    <cfset curLine=reader.readLine()>
<cfset reader.close()>
Subscribe via RSS ı Email
© 2023 - Jehiah Czebotar