function createfile(sfilename,scontent) set fso=server.CreateObject("scripting.filesystemobject") 'set f1=fso.opentextfile(sfilename,2,true,-1) 'append=8 only write=2 Unicode编码=-1 set f1=fso.opentextfile(sfilename,2,true) f1.write(scontent) f1.close set fso=nothing end function
function createfile(sfilename,scontent) Set objStream = Server.CreateObject("ADODB.Stream") With objStream .Open .Charset = "utf-8" .Position = objStream.Size .WriteText=scontent .SaveToFile sfilename,2 .Close End With Set objStream = Nothing end function
function readfile(sfilename) Set fso=server.CreateObject("scripting.filesystemobject") Set f = fso.OpenTextFile(sfilename, 1, true) if not f.AtEndOfStream then readfile = f.readAll Set f=nothing Set fso=nothing end function
Function readfile (sfilename) Dim f Set stm=server.CreateObject("adodb.stream") stm.Type=2 '以本模式读取 stm.mode=3 stm.charset="utf-8" stm.open stm.loadfromfile sfilename f=stm.readtext stm.Close Set stm=Nothing readfile=f End Function
关于文件编码和网页编码, 请参考“字符集Charset和文件编码Encoding的区别详解”。
其他样例程序
复制代码 代码如下:
'------------------------------------------------- '函数名称:ReadTextFile '作用:利用AdoDb.Stream对象来读取UTF-8格式的文本文件 '---------------------------------------------------- Function ReadFromTextFile (FileUrl,CharSet) Dim str Set stm=server.CreateObject("adodb.stream") stm.Type=2 '以本模式读取 stm.mode=3 stm.charset=CharSet stm.open stm.loadfromfile server.MapPath(FileUrl) str=stm.readtext stm.Close Set stm=nothing ReadFromTextFile=str End Function