Asp TextStream Object
Creating TextStream Object | Methods | Properties | ASP textstream object Example |
The asp TextStream Object
allows you to access the contents of the file as a text file.Once you have access
to the file you can read write information.Using this you can access the files
with .txt ,.html, .asp ,or.log extensions.
There are three ways that we can crate a text stream object.
Creating TextStream Object
1) Using OpenAsTextStream Method of file object
<%
dim strpathinfo,strphysicalpath
strvirtualpath=request.servervariables(“PATH_INFO”)
strphysicalpath=server.mapPath(strvirtualpath)
dim
objFSO,objFile,objTextStream
set objFSO=createobject(“scripting.filesystemobject”)
set objFile=objFSO.getfile(strphysicalpath)
Set objTextStream = objFile.OpenAsTextStream(Forreading,TrisatteUseDefault)
%>
2)
Using OpenTextFile method of FileSystemObject
<%
dim objFSO,objFile,objTextStream
set objFSO=createobject(“scripting.filesystemobject”)
Set objTextStream = objFSO.OpenTextFile(name,
iomode,create,format)
%>
3)
Using CreateTextFile method of FileSystemObject
<%
dim objFSO,objFile,objTextStream
set objFSO=createobject(“scripting.filesystemobject”)
Set objTextStream = objFSO.CreateTextFile(name,
overwrite,unicode)
%>
TextStream Object Methods
Method | Syntax | Description |
Read | objTextstream.Read(NoOfChar) | Reads a specified
number of characters from a TextStream file and returns the resulting string |
ReadLine | objTextstream.ReadLine | Reads an
entire line (up to, but not including, the newline character) from a TextStream file and returns the resulting string. |
ReadAll | objTextstream.ReadAll | Reads an
entire TextStream file and returns the resulting string. |
Write | objTextstream.Write(string) | Writes a
specified string to a TextStream file |
WriteLine | objTextstream.WriteLine(string) | Writes a
specified string and newline character to a TextStream file.string is optional.If omitted, a newline character is written to the file. |
WriteBlankLines | objTextstream.WriteBlankLines(lines) | Writes a
specified number of newline characters to a TextStream file. |
Skip | objTextstream.Skip(NoOfChar) | Skips a specified
number of characters when reading a TextStream file |
SkipLine | objTextstream.SkipLine | Skips the
next line when reading a TextStream file. |
Close | objTextstream.Close | Closes an
open TextStream file |
TextStream Object Properties
Property | Syntax | Description |
AtEndOfLine | objTextStream.AtEndOfLine | Read-only
property that returns True if the file pointer immediately precedes the end-of-line marker in a TextStream file; False if it does not |
AtEndOfStream | objTextStream.AtEndOfStream | Read-only
property that returns True if the file pointer is at the end of a TextStream file; False if it is not |
Column | objTextStream.Column | Read-only
property that returns the column number of the current character position in a TextStream file |
Line | objTextStream.Line | Read-only
property that returns the current line number in a TextStream file. |
The following is an example for reading source code of current page
<%
Const ForReading=1,ForWriting=2,ForAppending=3
Const tristateUseDefault=-2,TristateTrue=-1
, TristateFalse = 0
Dim
strVirtualPath , strPhysicalPath
strVirtualPath=Request.Querystring(“FileName”)
strPhysicalPath = server.MapPath(strVirtualPath)
Dim
objFso, objFiles
set objFso = createobject(“scripting.FileSystemObject”)
set objFile = objFSO.GetFile(strPhysicalPath)
%>
<html>
<head><title>Source code</title>
</head>
<body>
<%
dim objFileTextStream
set objFileTextStream = objFile.OpenAsTextStream(Forreading,tristateUseDefault)
Dim
strLine
Do While objFileTextStream.AtEndofStream<>True
strLine=server.HTMLEncode(objFileTextStream.ReadLine)
strLine=Replace(strLine,Chr(9),” ”)
response.write(strLine)
response.write(“<br>”)
Loop
objFileTextStream.close
%>
<p>
<hr>
<A HREF = “<%=strVirtualPath%>”>Return
to display files</a>
</body>
</html>