道招

asp读取txt文本文档内容至数组,并控制输出指定行(列)

如果您发现本文排版有问题,可以先点击下面的链接切换至老版进行查看!!!

asp读取txt文本文档内容至数组,并控制输出指定行(列)

针对很多朋友想知道如何用asp获取.txt文件的内容,并存入数组,输出指定行或列
<%
Sub ReadFiles() 
Dim objFSO 
Dim objText 
Dim ObjFile
Dim strTextContent
Dim objDrive
'创建一个文件操作对象实例
Set objFSO = CreateObject("Scripting.FileSystemObject")
'要打开的文件
ObjFile = "1.txt"
ObjFile=server.mappath(ObjFile)
Response.write ObjFile&"<br>" '输出文件完整路径及文件名
'Response.end
IF objFSO.FileExists(ObjFile) then 
    '打开TXT文件,并赋值给变量
    Set objText = objFSO.OpenTextFile(ObjFile,1)
    'strTextContent = objText.ReadAll()
    '循环读取数据
i=0
redim a(0)'定义数组 
'While not objText.AtEndOfStream '到文件的末尾
While not objText.AtEndOfLine '到一行的末尾
     strTextContent = objText.ReadLine()
     a(i)=strTextContent'将每行的值存入数组元素
      b=split(a(i),"=")'将每行的值存入新数组元素,以=为分隔符
      maxBound=Ubound(b)'获取每一行分割为多少个部分
      for j=0 to maxBound
      if j<maxBound then '输出该行的每个分割部分,用逗号隔开,最后一个部分时换行
      Response.write(b(j)) &" "
      else
      Response.write(b(j)) &"<br>"
      end if
      next
     'response.write a(i)&"_"&"<br>"
wend
i=i+1
    Call objText.Close
else
      response.write "文件不存在"
      response.end
end if
End Sub 
'调用过程,读取文件
readfiles()
%>
其中1.txt的内容如下:
姓名    编号
张三    135
李四    136
更新时间:
上一篇:下一篇:

相关文章

关注道招网公众帐号
友情链接
消息推送
道招网关注互联网,分享IT资讯,前沿科技、编程技术,是否允许文章更新后推送通知消息。
允许
不用了