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
Properties | Description |
AtEndOfLine | The AtEndOfLine property returns True if the file pointer is at the end of a line in the file, and False if not. |
AtEndOfStream | The AtEndOfStream property returns True if the file pointer is at the end of the file, and False if not. |
Column | The Column property returns the column number of the current character position in the file starting from 1. |
Line | The Line property returns the current line number in the file starting from 1. |
Methods
Methods | Description |
Close | The Close method closes a currently open file. Once closed, the file must be reopened before you can read from or write to it. |
Read | The Read method reads a specified number of characters from the file and returns them as a string. |
ReadAll | The ReadAll method reads the entire file and returns it as a single string. |
ReadLine | The ReadLine method reads a single line (up to a carriage return and line feed) from the file and returns the contents as a string. |
Skip | The Skip method skips specified number of characters when reading from the file. In conjunction with the Read method, the Skip method allows you to read a number of characters starting at a specific position. |
SkipLine | The SkipLine method skip the next line when reading from the file. |
Write | The Write method writes a specified string to the file. |
WriteLine | The WrtiteLine method writes a specified string and a new-line to the file. |
WriteBlankLines | The WriteBlankLines method writes a specified number of new-line characters to the file. |
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
%>