<p>如何使用ASP TextStream对象</p> <p>在下面的例子,我们将创建一个新的文本文件test.txt和写一些文本。首先,我们创建一个FileSystemObject对象的实例,然后使用CreateTextFile方法创建一个新的文本文件。的CreateTextFile返回一个TextStream对象,我们将用来写一些文本文件。</p> <p>&lt;%<br /> Dim objFSO, objTStream<br /> &#39;Create an instance of the FileSystemObject object<br /> Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)<br /> &#39;Create a text file named myFile.txt, replacing any existing one with the same name<br /> Set objTStream = objFSO.CreateTextFile(&quot;C:test.txt&quot;, True)<br /> &#39;Write some text to the file<br /> objTStream.WriteLine(&quot;WebCheatSheet ASP Tutorials!&quot;)<br /> objTStream.WriteBlankLines(1)<br /> objTStream.WriteLine(&quot;The TextStream Object&quot;)<br /> objTStream.WriteBlankLines(2)<br /> objTStream.WriteLine(&quot;The TextStream object provides sequential access to the contents of text files.&quot;)<br /> &#39;Close the TextStream object<br /> objTStream.Close<br /> &#39;Free up resources<br /> Set objTStream = nothing<br /> Set objFSO = nothing<br /> %&gt;<br /> 在下面的例子中,我们会认真阅读Test.txt文件内容。首先,我们创建FileSystemObject对象的实例,然后使用OpenTextFile方法打开Test.txt文件进行读取。此方法返回一个TextStream对象,我们将用来从文件中读取数据。我们会遍历每次读一行文件。</p> <p>&lt;%<br /> Dim objFSO, objTStream<br /> &#39;Create an instance of the FileSystemObject object<br /> Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)<br /> &#39;Opens for reading a text file named myFile.txt<br /> Set objTStream = objFSO.OpenTextFile(&quot;C:test.txt&quot;, 1)<br /> &#39;Read one line at a time until the end of the file is reached<br /> Do Until objTStream.AtEndOfStream<br /> Response.Write &quot;Line &quot; &amp; objTStream.Line &amp; &quot;: &quot; &amp; objTStream.ReadLine &amp; &quot;&lt;br /&gt;&quot;<br /> Loop<br /> &#39;Close the Textstream object<br /> objTStream.Close<br /> &#39;Free up resources<br /> Set objTStream = nothing<br /> Set objFSO = nothing<br /> %&gt;</p>
返回顶部 留言