Did you find this useful?
Socialize it today.


ASP TextStream Object

Friday, September 14, 2012

tags ASP

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

PropertyDescription
AtEndOfLineReturns True if the file pointer is at the end of a line, and False if not.
AtEndOfStreamReturns True if the file pointer is at the end of the file, and False if not.
ColumnReturns the column number of the current character position in the file starting from 1.
LineReturns the current line number.


Methods

MethodDescription
CloseCloses a currently open file. Once closed, the file must be reopened before you can read from or write to it.
ReadReads a specified number of characters from the file and returns them as a string.
ReadAllReads the entire file and returns it as a single string.
ReadLineReads a single line up to a carriage return and line feed from the file and returns the contents as a string.
SkipSkips specified number of characters when reading from the file.
SkipLineSkips the next line when reading from the file.
WriteWrites a specified string to the file.
WriteLineWrites a specified string and a new-line to the file.
WriteBlankLinesWrites 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.

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

Beginning ASP Databases ASP In A Nutshell Sams Teach Yourself Active Server Pages 3.0 in 21 Days