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
fileOpen(). Also accepted as filepath when passed as a named argument.
00 is the first line. Adobe's docs list this as required, but CF2025 accepts the call without it and reads from the first line.
startLine. Omit to read to the end of the file.
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.