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