描述
> 打开指定的文件并返回一个 TextStream 对象,此对象用于对文件进行读、写或追加操作。
语法
> object .OpenAsTextStream( [ iomode , [ format ]] )
>
> OpenAsTextStream 方法的语法有以下部分:
>
> 部分 | 描述
> ---|---
> object | 必选。应为 File 对象的名称。
> iomode | 可选。输入/输出模式,是下列三个常数之一: ForReading 、 ForWriting 或 ForAppending 。
> format | 可选。三个 Tristate 值之一,指出以何种格式打开文件。忽略此参数,则文件以 ASCII 格式打开。
设置
> iomode 参数可为下列设置之一:
>
> 常数 | 值 | 描述
> ---|---|---
> ForReading | 1 | 以只读模式打开文件。不能对此文件进行写操作。
> ForWriting | 2 | 以可读写模式打开文件。如果已存在同名的文件,则覆盖旧的文件。
> ForAppending | 8 | 打开文件并在文件末尾进行写操作。
>
> format 参数可为下列设置之一:
>
> 常数 | 值 | 描述
> ---|---|---
> TristateUseDefault | -2 | 以系统默认格式打开文件。
> TristateTrue | -1 | 以 Unicode 格式打开文件。
> TristateFalse | 0 | 以 ASCII 格式打开文件。
说明
> OpenAsTextStream 方法可提供与 FileSystemObject 对象的 OpenTextFile 方法相同的功能。另外,使用 OpenAsTextStream 方法可对文件进行写操作。 > > 以下代码举例说明如何使用 OpenAsTextStream 方法: > >> >> 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" '创建一个文件。 >> Set f = fso.GetFile("test1.txt") >> Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault) >> ts.Write "嗨,你好!" >> ts.Close >> Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault) >> TextStreamTest = ts.ReadLine >> ts.Close >> End Function >>