The TextStream object is used to 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
Property | Description |
AtEndOfLine | Returns True if the file pointer is at the end of a line, and False if not. |
AtEndOfStream | Returns True if the file pointer is at the end of the file, and False if not. |
Column | Returns the column number of the current character position in the file starting from 1. |
Line | Returns the current line number. |
Methods
Method | Description |
Close | Closes a currently open file. Once closed, the file must be reopened before you can read from or write to it. |
Read | Reads a specified number of characters from the file and returns them as a string. |
ReadAll | Reads the entire file and returns it as a single string. |
ReadLine | Reads a single line up to a carriage return and line feed from the file and returns the contents as a string. |
Skip | Skips specified number of characters when reading from the file. |
SkipLine | Skips the next line when reading from the file. |
Write | Writes a specified string to the file. |
WriteLine | Writes a specified string and a new-line to the file. |
WriteBlankLines | Writes a specified number of new-line characters to the file. |
Examples
In the following example, we will create a new text file test.txt and write some text to it. The first step is to 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
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objTStream = objFSO.CreateTextFile("C:\temp\test.txt", True)
objTStream.WriteLine("Hello, this is the first line!")
objTStream.WriteBlankLines(1)
objTStream.WriteLine("The TextStream Object is neat!")
objTStream.WriteBlankLines(2)
objTStream.WriteLine("The TextStream object provides access to the contents of text files.")
objTStream.Close
Set objTStream = nothing
Set objFSO = nothing
%>
In the next example, below we will read the contents of the test.txt file.
<%
Dim objFSO, objTStream
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objTStream = objFSO.OpenTextFile("C:\temp\test.txt", 1)
Do Until objTStream.AtEndOfStream
Response.Write("Line " & objTStream.Line & ": " & objTStream.ReadLine & "<br />")
Loop
objTStream.Close
Set objTStream = nothing
Set objFSO = nothing
%>
while some of the properties and methods listed above were not used in the example, you should be able integrate them in your code using a similar approach.
Did you find the page informational and useful? Share it using one of your favorite social sites.
Recommended Books & Training Resources