在下面的例子,我們將建立一個新的文字檔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
%>