首页 园地图库文章列表留言下载
现在位置:首页 -> 网络科技 -> 编程相关
使用ASP读出文本文件
作者:未知 来源:网络 更新日期:2005-10-14 阅读次数
文字 〖 自动滚屏(右键暂停)

很多朋友问到这样的问题,这里提供代码,该代码以只读方式打开,并可以显示行号,如果想修改的话,只需把打开时的属性改一下即可
<%
strtextfile = server.mappath("your_file.txt") ’取得文本文件的绝对路径
set objfso = server.createobject("scripting.filesystemobject") ’创建一个FSO对象的实例
%>
<textarea name="txtcontent" rows=10 cols=50 wrap="physical">
<%
set objstream = objfso.opentextfile(strtextfile,foreading) ’以只读方式打开文本文件
do while not objstream.atendofstream ’如果当前指针不在objstream流末尾
intlineNum = objstream.line ’获取文本文件的行数
strlinenum = right("00" & cstr(intlinenum,3)) ’对行数进行格式化
strlinetext = objstream.readline ’逐行读取文本内容
response.write strlinenum &":" & strlinetext & vbCrLf ’以行号:文本内容这种形式输出+回车换行
loop
objstream.close ’关闭stream对象
%>
</textarea>
-------------------------------------------------------------------

Ever want to know how to display the contents of a text document using ASP. Here is a easy way to read 

from a text file   



<!--Start of ASP Code---->



<%

’by James Seymour, http://jamesdot.orgmmkey.com



Dim write

Dim fileSysObj, tf, read



’ Read the read.txt 



’ Store the file name where the Information is stored into a variable called read



read = "read.txt"



’ Retrieve the fullpath of the read file



read = LEFT(Server.Mappath(Request.ServerVariables("PATH_INFO")), InStrRev(Server.Mappath

(Request.ServerVariables("PATH_INFO")), "\")) & read



’ Create an instance of FileSystem Object and store it into a variable called fileSysObj



Set fileSysObj = createObject("Scripting.FileSystemObject")



’ Check whether the read file exists



IF (fileSysObj.FileExists(read)) Then

’ if the file exists, then open it for reading

Set tf = filesysobj.OpenTextFile(read, 1)

read = tf.ReadLine

tf.Close

ELSE

’ if you can’t find read.text, display default message

read = "I can’t find the file read.txt! So this is my default message."



END IF

%>





’ table the displays the read.txt  file 

<div align="center">

<center>

<table border="0" width="40%" cellspacing="0" cellpadding="0">

<tr>

<td width="100%" bgcolor="#EEEECC"><B><%=read%></B>

</td>

</tr>

<tr>

<td width="100%">





<!-- End of the ASP --> 





use the code above, click and drag your mouse over the code to highlight it.   Then right click on the 

highlighted code and click "Copy."   Now you may paste it into your code editor

-------------------------------------------

文本文件question.txt内容:
第一行:问题1
第二行:问题1答案
第三行:问题2
第四行:问题2答案
我想把这四行内容读取出来分别赋给四个变量,怎么来? 

 


a= f.ReadAll
b = Split(a,vbCrLf)
x1=b(0)
x2=b(1)
..... 

 


You can use the FSO object to access the file 

For Example

Dim fs, f , ts, msg
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile("D:\share\dds.txt")
Set ts = f.OpenAsTextStream(1, 0)
msg = ts.ReadAll
ts.Close 


  -------------------------------------------------------

从文本文件中读取信息并存储入数据库 
 
<% 

dim fso,f1
Const ForReading = 1

set fso=createobject("Scripting.FileSystemObject")
’’使用fso对象读取信息。
set f1=fso.OpenTextFile("E:\doit\info2.txt", ForReading)
’’打开要读取的文件
dim conn,rst

set conn=server.createobject("adodb.connection")
’’建立数据库连接,执行操作
set rst=server.createobject("adodb.recordset")

conn.open "Driver={Microsoft Access Driver (*.mdb)};DBQ="&Server.mappath("db1.mdb")

rst.activeconnection=conn

dim fread

do while not f1.atEndOfStream 
’’用do... loop循环体插入数据
fread=f1.readline

line=split(fread,":::")

rst.source="insert into barcode1(barcode1) values ("&trim(line(0))&")"
rst.open
loop

f1.close
conn.close
’’’’all over
%>  

  
  
------------------------------------------

怎样用asp读取一个文本文件的内容? 这里介绍一种简便的读取一个文本文件的方法:

<!--Start of ASP Code---->

<%
’by James Seymour, http://jamesdot.orgmmkey.com

Dim write
Dim fileSysObj, tf, read

’ 读取文本read.txt 
’获得文件名字
read = "read.txt"
’读取文件的完整路径

read = LEFT(Server.Mappath(Request.ServerVariables("PATH_INFO")), InStrRev(Server.Mappath
(Request.ServerVariables("PATH_INFO")), "\")) & read


’新建一个fileSysObj对象

Set fileSysObj = createObject("Scripting.FileSystemObject")

’读取整个文本文件

IF (fileSysObj.FileExists(read)) Then
’如果文件存在,则打来并读取文件
Set tf = filesysobj.OpenTextFile(read, 1)
read = tf.ReadLine
tf.Close
ELSE
’如果找不到read.txt文件,则显示下面的信息!
read = "I can’t find the file read.txt! So this is my default message."

END IF
%>

’在下面表格中显示read.txt

<div align="center">
<center>
<table border="0" width="40%" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" bgcolor="#EEEECC"><%=read%>
</td>
</tr>
<tr>
<td width="100%">


<!-- End of the ASP -->  

------------------------

URL.txt
url1  url2  url3  url4  ……………

如何读取并格式化显示成一个两列的表格?
url1 url2
url3 url4
…………




方法有三种
1.操作文本文件时:
当你写入文本时,就写成行,两个一行,中间使用一个分割符
这样读取就很简单了。
2.使用你现在的方法,读取是READALL,然后使用SPLIT函数分割,根据UBOUND函数来循环操作就是了.
3.使用把URL写成XML形式的,读取更加简单,你可以去搜索相关的XML资料。




你可这样
1.把所有的文本内容读出赋值给变量
  txtCon=ts.ReadAll
2.用split分隔
  txtSingle=split(txtCon," ",-1)
3.获取单个值,并显示在表格中
<table border=1>
   <tr>
<%
  for i=0 to uBount(txtSingle)-1
     s=txtSingle(i)
%>
         <td><%=s%></td>
<%
         if (i mod 2)=1 then resposne.write "</tr><tr>"
  next
%>
   </tr>
</table>

-----我没调试,你试试看----------------- 
  
  
使用ASP读出文本文件并显示 
  读取一个文本文件并写出 Sun Aug 2 06:34:07 1998 

     (注:textStream有关写的METHOD 

     Write(STRING) 

     WriteLine(STRING) 

     WriteBlankLines(LINES) 

     ) 

     这是一个完整的程序 

     〈 html 〉 

     〈 head 〉 

     〈 http-equiv="Content-Type" content="text/html; charset=gb2312" 〉 

     〈 title 〉〈 /title 〉 

     〈 /head 〉 

     〈 body 〉 

     < % LANGUAGE = VBScript % >

     < % 

     Const ForReading = 1, ForWriting = 2, ForAppending = 8 

     Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0      

     Dim filename 

     filename = "test.txt" ’缺省相对路径是c:winnt 

     Set fs = CreateObject("Scripting.FileSystemObject") 

     Set f = fs.GetFile(filename) 

     Set readf = f.OpenAsTextStream(ForReading,TristateFalse) 

     ’第一个参数可选。输入/输出模式,是下列三个常数之一: 

     ’ ForReading=1只读、ForWriting=2 可读写或 ForAppending=3追加 

     ’第二个参数也为可选。三个 Tristate 值之一, 

     ’ 指出以何种格式打开文件。忽略此参数,则文件以 ASCII 

     ’格式打开。 TristateUseDefault=-2 以系统默认格式打开文件、 

     ’TristateTrue=-1 以 Unicode 格式打开文件或TristateFalse=0 

     ’以 ASCII 格式打开文件。 

     ’也可用OpenTextFile方法打开文件 

     s = readf.ReadLine 

     Do While readf.AtEndOfLine <> True 

         s = readf.ReadLine 

         Response.write s & "" ’逐行读文件并写出 

     Loop 

     readf.close

     % >

     < /body> 

     < /html> 

 这样就可以将文本文件读出并显示了。  


 

上篇文章: 李广
下篇文章:七夕礼物
相关文章:
  • 华速互联提供100M/ASP/FTP免费空间申请
  • 山城主机免费提供30M/ASP空间
  • 中商信息网提供ASP空间
  • 互连港湾提供50M免费ASP空间
  • 益恒网络提供50M免费ASP空间
  • 小刚娱乐网提供50M免费ASP空间
  • 蔚蓝网络提供80MB免费asp空间
  • ASP中的时间函数详细介绍
  • ASP常用函数
  • ASP中时间函数的使用
  • Asp中一些FSO方面的函数
  • 简单的Flash+ASP(最佳入门)
  • ASP连接11种数据库语法总结
  • 摘要:本文提供了优化 ASP 应用程序和 VBScript 的技巧。
  • 使用ASP生成HTML文件
  • ASP函数详解(2)
  • ASP函数详解(1)
  • 菜鸟详听asp中也能解压缩rar文件
  • asp应用之模板采用
  • 如何使用asp建立虚拟的ftp服务器
  • asp学习注意
  • 版权所有:文摘园地;本站资料均由站长收集,如果有侵权,请立即来信留言告诉我们,以免损害您的权益,谢谢!
    Copyright © 2004 文摘园地 http://wwww.mmkey.com All Rights Reserved Power by:HeadWind
    粤ICP备05098767号