当前位置: 移动技术网 > IT编程>开发语言>Asp > asp OpenTextFile文本读取与写入实例代码

asp OpenTextFile文本读取与写入实例代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

object.opentextfile(filename[, iomode[, create[, format]]]) 
参数 
object :必选项。应为 filesystemobject 对象的名称。 
filename :必选项。字符串表达式,指明要打开的文件名称。 
iomode :可选项。输入/输出模式,是下列三个常数之一:forreading,forwriting,或 forappending。 
create :可选项。boolean 值,指出当指定的 filename 不存在时是否能够创建新文件。允许创建新文件时为 true,否则为 false。默认值为 false。 
format :可选项。三个 tristate 值之一,指出以何种格式打开文件。若忽略此参数,则文件以 ascii 格式打开。
设置 
iomode 参数可为下列设置之一: 
常数                 值                        描述 
forreading        1         以只读模式打开文件。不能对此文件进行写操作。 
forwriting         2         以只写方式打开文件。不能对此文件进行读操作。 
forappending    8         打开文件并在文件末尾进行写操作。 
format 参数可为下列设置之一: 
常数                              值              描述 
tristateusedefault           -2        以系统默认格式打开文件。 
tristatetrue                   -1         以 unicode 格式打开文件。 
tristatefalse                    0         以 ascii 格式打开文件。 
说明 
以下代码举例说明如何使用 opentextfile 方法打开写文件:

复制代码 代码如下:

sub opentextfiletest    
  const forreading = 1, forwriting = 2, forappending = 8    
  dim fso, f    
  set fso = createobject("scripting.filesystemobject")    
  set f = fso.opentextfile("c:\testfile.txt", forwriting, true)    
  f.write "嗨,你好!"   
  f.close    
end sub  

移动技术网写的一个判断文件是否被更改过的代码

dim fso
set fso = createobject("scripting.filesystemobject")
set fn2=fso.getfile("e:\webroot\jb51\index2.htm")
flsize2=fn2.size
fldate2=fn2.datelastmodified
set fn=fso.getfile("e:\webroot\jb51\index.htm")
flsize1=fn.size
fldate1=fn.datelastmodified
if fso.fileexists("e:\webroot\jb51\index2.htm") and flsize2>50000 and fldate2>fldate1 then
'判断文件的大小,如果html文件重新生成需要判断是否更新过且文件不能小于50k
fso.getfile("e:\webroot\jb51\index2.htm").copy("e:\webroot\jb51\index.htm")
if err.number=0 then writehistory "成功"&now()&".........","log.txt"
end if
'日志写入函数
sub writehistory(hischars, path)
 const forreading = 1, forappending = 8
 dim fso, f
 set fso = createobject("scripting.filesystemobject")
 set f = fso.opentextfile(path, forappending, true)
 f.writeline hischars 
 f.close
end sub

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网