fileReadLines

Reads several lines from a file into an array, without loading the whole file into memory. Reads from startLine to the end of the file when count is omitted.

fileReadLines(file [, startLine [, count]]) → returns array

This function requires Adobe ColdFusion 2025 and up.  Not supported on Lucee, etc.

Argument Reference

file any
Required

Path to the file, or a file object returned by fileOpen(). Also accepted as filepath when passed as a named argument.

startLine numeric
Default: 0

The 0-based line number to start reading from, so 0 is the first line. Adobe's docs list this as required, but CF2025 accepts the call without it and reads from the first line.

count numeric

Number of lines to read. A negative value reads backward, returning that many lines before startLine. Omit to read to the end of the file.

Examples
Sample code invoking the fileReadLines function

startLine is 0-based, so 1 starts at the second line.

f = getTempFile(getTempDirectory(), "lines");
fileWrite(f, "line1#chr(10)#line2#chr(10)#line3#chr(10)#line4");
writeOutput(serializeJSON(fileReadLines(f, 1, 2)));

Expected Result: ["line2","line3"]

A negative count returns the lines before startLine.

f = getTempFile(getTempDirectory(), "lines");
fileWrite(f, "line1#chr(10)#line2#chr(10)#line3#chr(10)#line4");
writeOutput(serializeJSON(fileReadLines(f, 3, -2)));

Expected Result: ["line2","line3"]

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub