我想从文本文件中只读取第2行,但遗憾的是没有发现如何做到这一点。在找到行时,除了循环和中断之外还有其他解决方案吗?
答案 0 :(得分:2)
步骤1 - 将文件读取到变量:
<cffile action = "read" file = "yourFile" variable = "fileContents">
步骤2 - 将变量视为chr(10)分隔列表并获取第二行。
line2 = ListGetAt(fileContents, 2, chr(10));
答案 1 :(得分:2)
以下代码肯定有助于在Windows上查找第二行文本文件。
<cfset var readTextFile = fileRead(FileName) />
<cfset var endOfFirstLine = find(chr(13) & chr(10),readTextFile) />
<cfset var FirstLine = left(readTextFile,endOfFirstLine) />
<cfset removeFirstLine= replace(#readTextFile#,#FirstLine#,"","all")>
<cfset var endOfsecondLine = find(chr(13) & chr(10),removeFirstLine) />
<!---If endOfsecondLine less than 0 means file contains only two lines--->
<cfif endOfsecondLine GT 0>
<cfset var secoundLine = left(removeFirstLine,endOfsecondLine) />
<cfelse>
<cfset var secoundLine = removeFirstLine/>
</cfif>
<cfoutput>#secoundLine#</cfoutput>