<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>该属性表明是否已到达整个文本文件末尾。其值为&quot;TRUE&quot;或&quot;FALSE&quot;</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(&quot;c:testfile.txt&quot;, 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">&lt;%<br /> set myfileobject=server.createobject(&quot;scripting.filesystemobject&quot;)<br /> set mytextfile=myfileobject.opentextfile(&quot;c:mydirtest.txt&quot;)<br /> while not mytextfile.atendofstream<br /> response.write(mytextfile.readline)<br /> wend<br /> mytextfile.close<br /> %&gt;</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">&lt;%<br /> set myfileobject=server.CreateObject(&quot;Scripting.FileSystemObject&quot;)<br /> set mytextfile=myfileobject.CreateTextFile(&quot;c:mydirtest.txt&quot;)<br /> mytextfile.WriteLine<br /> mytextfile.close<br /> %&gt;</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>&lt;%<br /> set fs=Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)<br /> File = Server.MapPath(&quot;counter.txt&quot;)<br /> Set txtr = fs.OpenTextFile(File,1,true)&#39;打开只读文件file,如果不存在则新建<br /> counter=0&#39;将计数器归零<br /> If Not txtr.atEndOfStream Then&#39;先确定还没有到达结尾的位置<br /> Line = txtr.ReadLine &#39;读取一行数据<br /> else<br /> line=0 &#39;否则设置初始值为0<br /> End If<br /> counter=line+1 &#39;计数加1<br /> set txtw = fs.opentextfile(file,2,true) &#39;打开只写文件file<br /> txtw.write counter &#39;写入计数<br /> response.Write(&quot;</p> <center>您是第<b>&quot;&amp;counter&amp;&quot;</b>位访客!</center> &quot;)<br /> &#39;输出计数<br /> %&gt; <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>&lt;%</p> <p>response.Write(&quot;document.write(&quot;&quot;</p> <center>您是第<b>&quot;&amp;counter&amp;&quot;</b>位访客!</center> &quot;&quot;);&quot;) <p></p> <p>%&gt;</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>
返回顶部 留言