程序员的出路

alex.w的Blog

导航

ASP FSO详解-对文本文件的操作

Posted on 2008-10-15 15:11  alex.w  阅读(540)  评论(0)    收藏  举报
1. fso.OpenTextFile(file,n):
  
打开一个textfile对象  n的可选值:1 只读,2 可写,8 从文档末尾开始写
2. fso.CreateTextFile(file,true):
    创建一个textfile对象

 

Fso.openTextFile()的子方法

  • Read(Num): 从文本文件中读取Number个字符
  • ReadLine: 从文本文件中读取一行
  • ReadAll: 读取所有内容
  • Skip(Num): 从一个打开的文本文件中跳过Number个字符
  • Close: 关闭当前打开的文件
  • WriteLine(“str”): 写入整行信息    适用createTextFile方法
  • Write(“str”): 写入不换行的信息    适用createTextFile方法

 

Fso.openTextFile 的方法:
<%
whichfile=server.mappath("info.txt")
Set fso = CreateObject("Scripting.FileSystemObject")
Set txt = fso.OpenTextFile(whichfile,1)
rline = txt.ReadLine
rline = rline & "<br>" & txt.ReadLine
Response.Write rline
txt.Close
%>

 

OpenTextFile自定义函数:
1 读取文件中所有字符的函数
Function FsoFileRead(fileName)
fileName=server.mapPath(fileName)
set fso=server.createObject(“scripting.fileSystemObject”)
if not fso.fileExists(fileName) then exit function
set txt=fso.openTextFile(fileName,1)
while not txt.atEndOfStream
txtline=txt.readLine
txtline=server.HTMLENcode(txtline)
Response.write txtLine&”<br>”
Wend
End function
引用函数 call FSOFileRead("xxx文件") 即可

 

2 读取文件中某一行中所有字符的函数
Function FSOLineRead(fileName,lineNum)
If lineNum<1 then exit function
fileName=server.mapPath(fileName)
set fso=server.createObject(“scripting.fileSystemObject”)
if not fso.fileExists(fileName) then exit function
set txt=fso.openTextFile(fileName,1)
   if not txt.atEndOfStream then
   temp=txt.readall
   txt.close
   set txt=nothing
   temparray=split(temp,chr(13)&chr(10))
   if lineNum>ubound(temparray)+1 then
     exit function
   else
     FSOLineRead=temparray(lineNum-1)
end if
end if
response.write FSOLineRead
end function
函数的调用 call FSOlinedit("xxx文件",35) 表示显示xxx文件的第35行内容

 

3 读取文件中最后一行内容的函数
Function FSOLastLine(fileName)
fileName=server.mapPath(fileName)
set fso=server.createObject(“scripting.fileSystemObject”)
if not fso.fileExists(fileName) then exit function
set txt=fso.openTextFile(fileName,1)
if not txt.atEndOfStream then
temp=txt.readAll
txt.close
set txt=nothing
temparray=split(temp,chr(13)&chr(10))
FSOLastLine=temparray(ubound(temparray))
End if
Response.write FSOLastLine
End function

 

需要注意:无论是通过FSO打开驱动器、打开文件夹、打开文件,以及以后要接触到的打开数据库,都只能是打开绝对物理路径地址。但一般情况是上传到空间服务商那,不能很直接地了解到自己文件的所在位置,所以强烈推荐使用server.mappath方法

 

Fso.createTextFile 的方法:
<%
whichfile=server.mappath("info.txt")
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile(whichfile,True)
MyFile.WriteLine("My Name Is blues")
MyFile.WriteLine("My Sex Is Male")
MyFile.Close
%>
<a href="opentxt.asp">查看内容</a>

 

一个通过openTextFile和createTextFile实现的计数器例子
注site.txt的内容格式:A1B1C1D1  values (A=bluesdog.cn B=blueidea.com C=it365cn.com D=5d.cn)
<%
whichfile=server.mappath("site.txt")
set fso=createobject("Scripting.FileSystemObject")
set thisfile=fso.opentextfile(whichfile)
my_string=thisfile.readline

a_num = instr(my_string,"A")
b_num = instr(my_string,"B")
c_num = instr(my_string,"C")
d_num = instr(my_string,"D")
total_num = len(my_string)

a_value = mid(my_string,a_num+1,b_num-a_num-1)
b_value = mid(my_string,b_num+1,c_num-b_num-1)
c_value = mid(my_string,c_num+1,d_num-c_num-1)
d_value = mid(my_string,d_num+1,total_num-d_num)

select case request.form("website")
case "A": a_value=a_value+1
case "B": b_value=b_value+1
case "C": c_value=c_value+1
case "D": d_value=d_value+1
end select

mynew_string="A" & cstr(a_value) & "B" & cstr(b_value) & "C" & cstr(c_value) & "D" & cstr(d_value)
set newfile=fso.createtextfile(whichfile)
newfile.writeLine(mynew_string)
newfile.close
set fso=nothing
%>
当前投票:<br>
bluesdog.cn:<%=a_value%><br>
blueidea.com:<%=b_value%><br>
it356cn.com:<%=c_value%><br>
5d.cn:<%=d_value%><br>
<a href="website.html">返回继续</a>