如何使用ASP TextStream對象

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.