TextStream object
read file
This example demonstrates how to use the OpenTextFile method of FileSystemObject to create a TextStream object. The ReadAll method of the TextStream object obtains the content from the opened text file.
This sample code is as follows:
<html>
<body>
<p>This is the text in the text file:</p>
<%
Set fs=Server.CreateObject(Scripting.FileSystemObject)
Set f=fs.OpenTextFile(Server.MapPath(/example/aspe/testread.txt), 1)
Response.Write(f.ReadAll)
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>
The results of this example are as follows:
This is the text in the text file:
Hello! How are you today?
Read a section of a text file
This example demonstrates how to read only part of a text stream file.
This sample code is as follows:
<html>
<body>
<p>This is the first 5 characters read from the text file:</p>
<%
Set fs=Server.CreateObject(Scripting.FileSystemObject)
Set f=fs.OpenTextFile(Server.MapPath(testread.txt), 1)
Response.Write(f.Read(5))
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>
The results of this example are as follows:
Here are the first 5 characters read from the text file:
Hello
Read a line from a text file
This example demonstrates how to read a line from a text stream file.
This sample code is as follows:
<html>
<body>
<p>This is the first line read from the text file:</p>
<%
Set fs=Server.CreateObject(Scripting.FileSystemObject)
Set f=fs.OpenTextFile(Server.MapPath(testread.txt), 1)
Response.Write(f.ReadLine)
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>
The results of this example are as follows:
This is the first line read from the text file:
Hello!
Read all lines of text file
This example demonstrates how to read all lines from a text stream file.
This sample code is as follows:
<html>
<body>
<p>This is all the lines read from the text file:</p>
<%
Set fs=Server.CreateObject(Scripting.FileSystemObject)
Set f=fs.OpenTextFile(Server.MapPath(testread.txt), 1)
do while f.AtEndOfStream = false
Response.Write(f.ReadLine)
Response.Write(<br>)
loop
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>
The results of this example are as follows:
This is all the lines read from the text file:
Hello!
How are you today?
Skip part of text file
This example demonstrates how to skip a specified number of characters when reading a text stream file.
This sample code is as follows:
<html>
<body>
<p>The first 4 characters in the text file are omitted:</p>
<%
Set fs=Server.CreateObject(Scripting.FileSystemObject)
Set f=fs.OpenTextFile(Server.MapPath(testread.txt), 1)
f.Skip(4)
Response.Write(f.ReadAll)
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>
The results of this example are as follows:
The first 4 characters in the text file are omitted:
o! How are you today?
Skip a line of text file
This example demonstrates how to skip a line when reading a text stream file.
This sample code is as follows:
<html>
<body>
<p>The first line in the text file is omitted:</p>
<%
Set fs=Server.CreateObject(Scripting.FileSystemObject)
Set f=fs.OpenTextFile(Server.MapPath(testread.txt), 1)
f.SkipLine
Response.Write(f.ReadAll)
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>
The results of this example are as follows:
The first line in the text file is omitted:
How are you today?
Return the number of rows
This example demonstrates how to return the current line number in a text stream file.
This sample code is as follows:
<html>
<body>
<p>Here are all the lines in the text file (with line numbers):</p>
<%
Set fs=Server.CreateObject(Scripting.FileSystemObject)
Set f=fs.OpenTextFile(Server.MapPath(testread.txt), 1)
do while f.AtEndOfStream = false
Response.Write(Line: & f.Line & )
Response.Write(f.ReadLine)
Response.Write(<br>)
loop
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>
The results of this example are as follows:
Here are all the lines in the text file (with line numbers):
Line:1 Hello!
Line:2 How are you today?
Get the number of columns
This example demonstrates how to get the column number of the current character in the file.
This sample code is as follows:
<html>