最近在為網(wǎng)站添加一個內(nèi)容,涉及到在線讀取和寫入UTF-8編碼的文件,才發(fā)現(xiàn)FSO對象只能讀寫ANSI編碼的文件,無法讀寫UTF-8編碼的文件,琢磨了半天,終于在網(wǎng)上找到一個很實用的函數(shù),完整的代碼如下: '函數(shù)名稱:ReadTextFile '作用:利用Adodb.Stream對象來讀取UTF-8編碼的文件 '示例:Call ReadFromTextFile("File/FileName.htm","UTF-8") Function ReadFromTextFile(FileUrl,CharSet) dim str set stm=server.CreateObject("adodb.stream") stm.Type=2 '2-文本模式讀取,1-二進制模式 stm.mode=3 '3-讀寫,1-讀,2-寫 stm.charset=CharSet ‘unicode|utf-8;Ascii; gb2312; big5; gbk stm.open stm.loadfromfile server.MapPath(FileUrl) str=stm.readtext stm.Close set stm=nothing ReadFromTextFile=str End Function '函數(shù)名稱:WriteToTextFile '作用:利用Adodb.Stream對象來寫入UTF-8編碼的文件 '示例:Call WriteToTextFile("File/FileName.htm",Content,"UTF-8") Sub WriteToTextFile(FileUrl,byval Str,CharSet) set stm=server.CreateObject("adodb.stream") stm.Type=2 '2-文本模式讀取,1-二進制模式 stm.mode=3 '3-讀寫,1-讀,2-寫 stm.charset=CharSet ‘unicode|utf-8;Ascii; gb2312; big5; gbk; stm.open stm.WriteText str stm.SaveToFile server.MapPath(FileUrl),2 ‘2可省略,adSaveCreateNotExist =1 , adSaveCreateOverWrite =2 stm.flush stm.Close set stm=nothing End Sub |
|