![]() |
WebCheatSheet.com |
![]() |
ASP TextStream Object |
The TextStream object provides sequential access to the contents of text files. This allows you to read, write, or append characters or lines to a text file. The properties and methods of the TextStream object are as follows: Properties
Methods
How to use the ASP TextStream Object In the example below we will create a new text file test.txt and write some text to it. At first we create an instance of the FilesystemObject object and then use the CreateTextFile method to create a new text file. The CreateTextFile returns a TextStream object that we will use to write some text to the file. <%
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:\test.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 %> In the following example we will read the test.txt file contents. At first we create an instance of the FileSystemObject object and then use the OpenTextFile method to open test.txt file for reading. This method returns a TextStream object that we will use to read data from the file. We will iterate through the file reading one line at a time. <% 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:\test.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 %> |
![]() |
![]() |
Copyright © 2005-2007 www.WebCheatSheet.com All Rights Reserved. |