ASP中IO操作读写文件一般有2种方法:一是FSO对象,二是ADODB.STREAM对象。
用FSO读取和创建文件比较常用,但是要生成UTF-8编码的文件,没测试成功。通过object.CreateTextFile(filename[, overwrite[, unicode]])方法生成的文件编码可以为unicode,虽然文件可以正常浏览查看,但搜索引擎抓取到的却是乱码。下面我们来看看UTF-8与UNICODE的区别。
各种编码的定义:
1.字符编码、内码、汉字编码
ASCII、GB2312、GBK、GB18030向下兼容,即同一个字符在这些方案中总是有相同的编码,后面的标准支持更多的字符。
2.Unicode、UCS、UTF
Unicode只与ASCII兼容,与GB码不兼容。Unicode也是一种字符编码方式,不过它是由国际组织设计,可以容纳全世界所有语言文字的编码方案。其中UCS规定怎么用多个字节表示各种文字,UTF则规定怎么传输这些编码。现在一般使用UCS-2,即用两个字节进行编码。而所说的UFT-8就是以8位为单元对UCS进行编码。
3.cp936是什么?
Unicode统一了编码,但是如何兼容各国的文字编码就需要用到codepage,codepage就是各国的文字编码和Unicode之间的映射表,比如简体中文和Unicode的映射表就是CP936。
从936中随意取一行,例如:
0x9993 0x6ABD #CJK UNIFIED IDEOGRAPH
前面的编码是GBK的编码,后面的是Unicode,通过查这张表,就能简单的实现GBK和Unicode之间的转换。
这时要创建UTF-8编码的文件,可使用ADODB.STREAM对象。下面提供2个函数,分别用于读和写:
- function LoadContent(path,usefso)
- if usefso then
- Set fs = CreateObject("Scripting.FileSystemObject")
- set f=fs.OpenTextFile(path)
- strContent=f.ReadAll
- f.close:set f=nothing
- set fs=nothing
- else
- on error resume next
- set stream=Server.CreateObject("ADODB.Stream")
- If Err.Number=-2147221005 Then
- Response.Write "<div align='center'>不支持ADODB.Stream</div>"
- Err.Clear
- Response.End
- End If
- with stream
- .type=2
- .mode=3
- .Open
- .LoadFromFile path
- .Charset = "GB2312"
- .Position = 2
- strContent=.ReadText
- end with
- stream.close:stream=nothing
- end if
- 'LoadContent=strContent
- 'response.write strContent
- LoadContent=strContent
- end function
- function WriteContent(path,content,overrite,usefso)
- if usefso=true then
- Set fs = CreateObject("Scripting.FileSystemObject")
- if overrite=true then
- set f=fs.CreateTextFile(path,overrite)
- else
- set f=fs.OpenTextFile(path,8)
- end if
- f.writeLine(content)
- f.close:set f=nothing
- set fs=nothing
- else
- On Error Resume Next
- Set objStream = Server.CreateObject("ADODB.Stream")
- If Err.Number=-2147221005 Then
- Response.Write "<div align='center'>不支持ADODB.Stream</div>"
- Err.Clear
- Response.End
- End If
- With objStream
- .Type = 2
- .Open
- .Charset = "utf-8" '这里指定编码格式
- .Position = objStream.Size
- .WriteText = content
- 'if override=true then
- .SaveToFile path,2
- 'else
- ' .SaveToFile path,1
- 'end if
- .Close
- End With
- Set objStream = Nothing
- end if
- end function
- <%
- dim mytxt,fname,fpath
- fname="1.txt"
- '.表示当前目录
- fpath=Server.MapPath(".")
- set fso=server.createobject("scripting.filesystemobject")
- '参数说明:第一个true表示覆盖已有同名文件,否则为不覆盖;
- '第个false表示创建格式为ASCII格式,如果true则为Unicode
- set mytxt=fso.createtextfile(fPath & "/" & fname,true,false)
- mytxt.write "此处为写入1.txt中的内容"&chr(13)
- mytxt.close
- response.write "当前目录下建立新文件1.txt并写入成功<br>"
- set mytxt=nothing
- set fso=nothing
- %>
-
- <%
- fname="1.txt"
- fpath=Server.MapPath(".")
- set fso=server.createobject("scripting.filesystemobject")
- set mytxt=fso.opentextfile(fPath & "/" & fname,8,true,-2)
- '参数说明:8读取+追加(1读取,2写入),true文件不存在时创建新文件,-2系统默认格式ASCII(0为ASCII
- '-1为Unicode格式
- response.write "打开指定文件1.txt成功<br>"
- mytxt.write(request.servervariables("remote_host"))
- mytxt.close
- set mytxt=nothing
- set fso=nothing
- response.write "写入客户端ip到指定文件1.txt成功<br>"
- %>