asp下用ADODB.Stream代替FSO读取文本文件
程序员文章站
2022-10-27 19:13:11
'文件内容读取. 复制代码 代码如下:function loadfile(byval file) dim objstream on ...
'文件内容读取.
function loadfile(byval file)
dim objstream
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
.mode = 3
.open
.loadfromfile server.mappath(file)
if err.number<>0 then
response.write "<div align='center'>文件<font color='#ff0000'>"&file&"</font>无法被打开,请检查是否存在!</font></div>"
err.clear
response.end
end if
.charset = "gb2312"
.position = 2
loadfile = .readtext
.close
end with
set objstream = nothing
end function
'存储内容到文件
sub savetofile(byval strbody,byval file)
dim objstream
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 = "gb2312"
.position = objstream.size
.writetext = strbody
.savetofile server.mappath(file),2
.close
end with
set objstream = nothing
end sub
调用示例
<%
writefile "aaa.txt", "www.jb51.net", "utf-8"
response.write readfile("aaa.txt", "utf-8")
%>
复制代码 代码如下:
function loadfile(byval file)
dim objstream
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
.mode = 3
.open
.loadfromfile server.mappath(file)
if err.number<>0 then
response.write "<div align='center'>文件<font color='#ff0000'>"&file&"</font>无法被打开,请检查是否存在!</font></div>"
err.clear
response.end
end if
.charset = "gb2312"
.position = 2
loadfile = .readtext
.close
end with
set objstream = nothing
end function
'存储内容到文件
复制代码 代码如下:
sub savetofile(byval strbody,byval file)
dim objstream
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 = "gb2312"
.position = objstream.size
.writetext = strbody
.savetofile server.mappath(file),2
.close
end with
set objstream = nothing
end sub
调用示例
复制代码 代码如下:
<%
writefile "aaa.txt", "www.jb51.net", "utf-8"
response.write readfile("aaa.txt", "utf-8")
%>