如何用asp读文本文件。

guozhiw 2003-06-14 01:32:56
等待中。。。
...全文
52 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ngod 2003-06-14
  • 打赏
  • 举报
回复
method2
<% Option Explicit

Const Filename = "/readme.txt" ' file to read

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

' Create a filesystem object
Dim FSO
set FSO = server.createObject("Scripting.FileSystemObject")

' Map the logical path to the physical system path
Dim Filepath
Filepath = Server.MapPath(Filename)

if FSO.FileExists(Filepath) Then

Set TextStream = FSO.OpenTextFile(Filepath, ForReading, False, _
TristateUseDefault)
' Read file in one hit

Dim Contents
Contents = TextStream.ReadAll
Response.write "<pre>" & Contents & "</pre><hr>"
TextStream.Close
Set TextStream = nothing

Else

Response.Write "<h3><i><font color=red> File " & Filename &_
" does not exist</font></i></h3>"

End If

Set FSO = nothing
%>
Ngod 2003-06-14
  • 打赏
  • 举报
回复
method1
<% Option Explicit

Const Filename = "/readme.txt" ' file to read

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

' Create a filesystem object
Dim FSO
set FSO = server.createObject("Scripting.FileSystemObject")

' Map the logical path to the physical system path
Dim Filepath
Filepath = Server.MapPath(Filename)

if FSO.FileExists(Filepath) Then

' Get a handle to the file
Dim file
set file = FSO.GetFile(Filepath)

' Get some info about the file
Dim FileSize
FileSize = file.Size

Response.Write "<p><b>File: " & Filename & " (size " & FileSize &_
" bytes)</b></p><hr>"
Response.Write "<pre>"

' Open the file
Dim TextStream
Set TextStream = file.OpenAsTextStream(ForReading, _
TristateUseDefault)

' Read the file line by line
Do While Not TextStream.AtEndOfStream
Dim Line
Line = TextStream.readline

' Do something with "Line"
Line = Line & vbCRLF

Response.write Line
Loop


Response.Write "</pre><hr>"

Set TextStream = nothing

Else

Response.Write "<h3><i><font color=red> File " & Filename &_
" does not exist</font></i></h3>"

End If

Set FSO = nothing
%>
fason 2003-06-14
  • 打赏
  • 举报
回复
http://www.aspsky.net/article/index.asp?classid=2&Nclassid=33
freezh 2003-06-14
  • 打赏
  • 举报
回复
WriteLine为写
readline为读
freezh 2003-06-14
  • 打赏
  • 举报
回复
这是一个读写的例 子

Sub ReadFiles
Dim fso, f1, ts, s
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
'' 写一行。
Response.Write "Writing file <br>"
f1.WriteLine "Hello World"
f1.WriteBlankLines(1)
f1.Close
'' 读取文件的内容。
Response.Write "Reading file <br>"
Set ts = fso.OpenTextFile("c:\testfile.txt", ForReading)
s = ts.ReadLine
Response.Write "File contents = ''" & s & "''"
ts.Close
End Sub
ddboy 2003-06-14
  • 打赏
  • 举报
回复
<%
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(FileName, 1, False)
str = f.ReadAll
f.close
Set fso = Nothing
%>
fso其它方法或对象,参见VBScript帮助
freezh 2003-06-14
  • 打赏
  • 举报
回复
Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
叶子哟 2003-06-14
  • 打赏
  • 举报
回复
fso没问题!新建修改均可以
freezh 2003-06-14
  • 打赏
  • 举报
回复
使用fso

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧