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