//--------------读例子
Set fso = CreateObject("scripting.FileSystemObject") //绑定fso对象
If fso.FileExists(s_FileUrl) then //用.FileExists方法检查文件是否存在
Set myfile = fso.openTextFile(s_FileUrl, 1, false) //用.open方法打开文件
While Not myfile.AtEndOfLine //用文件的 .AtEndOfLine 方法检查是否到了文件末尾
LineText = myfile.ReadLine //用文件的 .ReadLine方法读一行
Wend
myfile.close
Set myfile = Nothing
End If
Set fso = Nothing
//---------------写例子
Set fso = CreateObject("scripting.FileSystemObject") //绑定fso对象
If fso.FileExists(s_FileUrl) then //用.FileExists方法检查文件是否存在
Set myfile = fso.openTextFile(s_FileUrl, 1, false) //用.open方法打开文件
While Not myfile.AtEndOfLine //用文件的 .AtEndOfLine 方法检查是否到了文件末尾
myfile.write("文本内容") //用文件的 .write方法写一行
Wend
myfile.close
Set myfile = Nothing
End If
Set fso = Nothing
注:
object.OpenTextFile(filename[, iomode[, create[, format]]])
参数
object 必选项。object 应为 FileSystemObject 的名称。
filename 必选项。指明要打开文件的字符串表达式。
iomode 可选项。可以是三个常数之一:ForReading 、 ForWriting 或 ForAppending 。
create 可选项。Boolean 值,指明当指定的 filename 不存在时是否创建新文件。如果创建新文件则值为 True 如果不创建则为 False 。如果忽略,则不创建新文件。
format 可选项。使用三态值中的一个来指明打开文件的格式。如果忽略,那么文件将以 ASCII 格式打开。
设置
iomode 参数可以是下列设置中的任一种:
常数 值 描述
ForReading 1 以只读方式打开文件。不能写这个文件。
ForWriting 2 以写方式打开文件
ForAppending 8 打开文件并从文件末尾开始写。
format 参数可以是下列设置中的任一种:
值 描述
TristateTrue 以 Unicode 格式打开文件。
TristateFalse 以 ASCII 格式打开文件。
TristateUseDefault 使用系统默认值打开文件。