<h1>asp FSO 读写文件本文件实现代码</h1>
<div class="bodyInfo" id="suad">
<div class="bda_d" id="bda_d"></div>
</div>
<div class="bodyContent">
<div class="keywords">asp己经过时有一段时间了,我来讲述一下利用asp fso来实现文件读写操作,有需要学习的朋友可参考参考。</div>
<div class="floatAd" id="c_ads5"></div>
<div class="rightAd" id="c_ads6"></div>
<p>1.AtEndOfStream</p>
<p>该属性表明是否已到达整个文本文件末尾。其值为"TRUE"或"FALSE"</p>
<p>2.CreateTextFile</p>
<p>用来创建新的文本文件</p>
<p>3.OpenTextFile()方法中的参数</p>
<p>saucer(思归)所写的:</p>
<table align="center" border="0" cellpadding="1" cellspacing="1" width="620">
<tbody>
<tr>
<td bgcolor="#ffe7ce" height="27" width="464">代码如下</td>
<td align="center" bgcolor="#ffe7ce" width="109">复制代码</td>
</tr>
<tr>
<td bgcolor="#ffffff" class="copyclass" colspan="2" id="copy2204" valign="top">Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True)</td>
</tr>
</tbody>
</table>
<p>ForWriting值为2,表示打开一个可写的文本文件<br />
为1,表示打开一个可读的文本文件<br />
为8,表示要附加数据</p>
<p>True是表示如果没有这个文件,将新建一个<br />
这个参数可选True或False,表明文件不存在时是否创建</p>
<p>实例1</p>
<p>读文件:</p>
<table align="center" border="0" cellpadding="1" cellspacing="1" width="620">
<tbody>
<tr>
<td bgcolor="#ffe7ce" height="27" width="464">代码如下</td>
<td align="center" bgcolor="#ffe7ce" width="109">复制代码</td>
</tr>
<tr>
<td bgcolor="#ffffff" class="copyclass" colspan="2" id="copy9416" valign="top"><%<br />
set myfileobject=server.createobject("scripting.filesystemobject")<br />
set mytextfile=myfileobject.opentextfile("c:mydirtest.txt")<br />
while not mytextfile.atendofstream<br />
response.write(mytextfile.readline)<br />
wend<br />
mytextfile.close<br />
%></td>
</tr>
</tbody>
</table>
<p><br />
写文件:</p>
<table align="center" border="0" cellpadding="1" cellspacing="1" width="620">
<tbody>
<tr>
<td bgcolor="#ffe7ce" height="27" width="464">代码如下</td>
<td align="center" bgcolor="#ffe7ce" width="109">复制代码</td>
</tr>
<tr>
<td bgcolor="#ffffff" class="copyclass" colspan="2" id="copy9372" valign="top"><%<br />
set myfileobject=server.CreateObject("Scripting.FileSystemObject")<br />
set mytextfile=myfileobject.CreateTextFile("c:mydirtest.txt")<br />
mytextfile.WriteLine<br />
mytextfile.close<br />
%></td>
</tr>
</tbody>
</table>
<p><br />
实例2</p>
<p>FSO打造最简单的访问计数器</p>
<table align="center" border="0" cellpadding="1" cellspacing="1" width="620">
<tbody>
<tr>
<td bgcolor="#ffe7ce" height="27" width="464">代码如下</td>
<td align="center" bgcolor="#ffe7ce" width="109">复制代码</td>
</tr>
<tr>
<td bgcolor="#ffffff" class="copyclass" colspan="2" id="copy6746" valign="top">
<p><%<br />
set fs=Server.CreateObject("Scripting.FileSystemObject")<br />
File = Server.MapPath("counter.txt")<br />
Set txtr = fs.OpenTextFile(File,1,true)'打开只读文件file,如果不存在则新建<br />
counter=0'将计数器归零<br />
If Not txtr.atEndOfStream Then'先确定还没有到达结尾的位置<br />
Line = txtr.ReadLine '读取一行数据<br />
else<br />
line=0 '否则设置初始值为0<br />
End If<br />
counter=line+1 '计数加1<br />
set txtw = fs.opentextfile(file,2,true) '打开只写文件file<br />
txtw.write counter '写入计数<br />
response.Write("</p>
<center>您是第<b>"&counter&"</b>位访客!</center>
")<br />
'输出计数<br />
%>
<p></p>
</td>
</tr>
</tbody>
</table>
<p>在需要统计和显示计数的地方可以用include的方式引入这个文档。</p>
<table align="center" border="0" cellpadding="1" cellspacing="1" width="620">
<tbody>
<tr>
<td bgcolor="#ffe7ce" height="27" width="464">代码如下</td>
<td align="center" bgcolor="#ffe7ce" width="109">复制代码</td>
</tr>
<tr>
<td bgcolor="#ffffff" class="copyclass" colspan="2" id="copy3243" valign="top">
<p><!--#include file="counter.asp"--></p>
</td>
</tr>
</tbody>
</table>
<p><br />
但是如果是html之类静态页面,则可以通过script脚本的方式来使用这个简单的计数器。</p>
<p>那么Counter.asp则需要这么写了</p>
<table align="center" border="0" cellpadding="1" cellspacing="1" width="620">
<tbody>
<tr>
<td bgcolor="#ffe7ce" height="27" width="464">代码如下</td>
<td align="center" bgcolor="#ffe7ce" width="109">复制代码</td>
</tr>
<tr>
<td bgcolor="#ffffff" class="copyclass" colspan="2" id="copy4373" valign="top">
<p><%</p>
<p>response.Write("document.write(""</p>
<center>您是第<b>"&counter&"</b>位访客!</center>
"");")
<p></p>
<p>%></p>
</td>
</tr>
</tbody>
</table>
<p>需要引用的html静态页里可以通过Script脚本来使用计数器:</p>
<table align="center" border="0" cellpadding="1" cellspacing="1" width="620">
<tbody>
<tr>
<td bgcolor="#ffe7ce" height="27" width="464">代码如下</td>
<td align="center" bgcolor="#ffe7ce" width="109">复制代码</td>
</tr>
<tr>
<td bgcolor="#ffffff" class="copyclass" colspan="2" id="copy1597" valign="top">
<p><script src="counter.asp"></script></p>
</td>
</tr>
</tbody>
</table>
</div>