ASP OpenAsTextStream Method

Definition and Usage

The OpenAsTextStream method opens the specified file and returns a TextStream object for accessing the file.

Syntax:

FileObject.OpenAsTextStream(mode,format)
Parameter Description
mode Optional. How to open the file (input/output mode).
  • 1 = ForReading - Open the file in read-only mode. Write operations cannot be performed on this file.
  • 2 = ForWriting - Open the file in read/write mode. If a file with the same name already exists, the old file will be overwritten.
  • 8 = ForAppending - Open the file and write to the end of the file.
format Optional. The format to open the file in. If this parameter is ignored, the file is opened in ASCII format.
  • 0 = TristateFalse - Default. Open the file in ASCII format.
  • -1 = TristateTrue - Open the file in Unicode format.
  • -2 = TristateUseDefault - Open the file using the system default format.

Example

<%
dim fs,f,ts
set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.GetFile("c:\test.txt")
Set ts=f.OpenAsTextStream(ForWriting)
ts.Write("Hello World!")
ts.Close
Set ts=f.OpenAsTextStream(ForReading)
Response.Write(ts.ReadAll)
ts.Close
set ts=nothing
set f=nothing
set fs=nothing
%>

Output:

Hello World!