Greg's Blog

helping me remember what I figure out

CFMX Wrapper for Java File Reader

| Comments

Here is a code snippet I stumbled across recently. I modified it slightly to simply output the line that is being read. It serves as a simple example as to how you can read a file and then deal with it line by line.

<cfscript>
var linecount = 0;
filename = “/absolutePath/toYourFile”;
fileReader = createObject(“java”, “java.io.FileReader”);
fileReader = fileReader.init(filename);
lineReader = createObject(“java”,”java.io.LineNumberReader”);
lineReader = lineReader.init(fileReader);
line = lineReader.readLine(); //Read first line, if any into variable line
while (isDefined(“line”)) {
lineCount = lineCount + 1;

//Process the variable line
WriteOutput(lineCount & “. ” & line & “<br />”);
line = lineReader.readLine(); //Read the next line, if any
}
</cfscript>