The following code illustrates the use of the
OpenAsTextStream method:
JScript
function TextStreamTest( )
{
var fso, f, ts, s;
var ForReading = 1, ForWriting = 2, ForAppending = 8;
var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0;
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CreateTextFile( "test1.txt" ); // Create a file.
f = fso.GetFile("test1.txt");
ts = f.OpenAsTextStream(ForWriting, TristateUseDefault);
ts.Write( "Hello World" );
ts.Close( );
ts = f.OpenAsTextStream(ForReading, TristateUseDefault);
s = ts.ReadLine( );
ts.Close( );
return(s);
}
VBScript
Function TextStreamTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, f, ts
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "test1.txt" ' Create a file.
Set f = fso.GetFile("test1.txt")
Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
ts.Write "Hello World"
ts.Close
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
TextStreamTest = ts.ReadLine
ts.Close
End Function
PowerBASIC
FUNCTION TextStreamTest (BYVAL strPathSpec AS STRING) AS STRING
LOCAL fso AS IFileSystem
LOCAL f AS IFile
LOCAL ts AS ITextStream
fso = NEWCOM ("Scripting.FileSystemObject")
fso.CreateTextFile UCODE$("test1.txt"), %VARIANT_TRUE
f = fso.GetFile(UCODE$("test1.txt"))
ts = f.OpenAsTextStream(%ForWriting, %TristateUseDefault)
ts.Write UCODE$("Hello World")
ts.Close
ts = f.OpenAsTextStream(%ForReading, %TristateUseDefault)
FUNCTION = ACODE$(ts.ReadLine)
ts.Close
END FUNCTION