asp读写txt2
2014-08-26来源:易贤网

<%

'读取a.txt文件的文本内容

Function LoadFile(ByVal File)

Dim objStream

On Error Resume Next '容错

Set objStream = Server.CreateObject("ADODB.Stream")

If Err.Number=-2147221005 Then '检测是否支持ADODB.Stream

Response.Write "您的主机不支持ADODB.Stream,不能使用本程序!"

Err.Clear

Response.End

End If

With objStream

.Type = 2

.Mode = 3

.Open

.LoadFromFile Server.MapPath(File)

If Err.Number<>0 Then

Response.Write "文件"&File&"无法被打开,请检查是否存在!</font></div>"

Err.Clear

Response.End

End If

.Charset = "GB2312"

.Position = 2

LoadFile = .ReadText

.Close

End With

Set objStream = Nothing

End Function

'输出内容

response.write LoadFile("a.txt")

'存储内容到a.txt文件

Sub SaveToFile(ByVal strBody,ByVal File)

Dim objStream

On Error Resume Next '容错

Set objStream = Server.CreateObject("ADODB.Stream")

If Err.Number=-2147221005 Then '检测是否支持ADODB.Stream

Response.Write "您的主机不支持ADODB.Stream,不能使用本程序!"

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

'将“123456789”写入文件a.txt

SaveToFile "123456789","a.txt"

%>

更多信息请查看IT技术专栏

推荐信息