当前位置: 首页 > 图文教程 > 网络编程 > ASP > ASP实例教程:TextStream对象

ASP
获取字符中中文首字字符
系统分配随机密码
MusicGet 类
数据库记录的删除,delete好还是update好?
asp提高首页性能的一个技巧
研究动网得到的一些动网参数
在ASP中连接MySQL数据库,最好的通过ODBC方法
FSO文件对象介绍及常用函数
分享一段代码show.asp?id=26变成show/?26的形式
ASP+XML留言板介绍
忠网广告 系统 用到的几个函数
asp读取xml文件
把无限级分类生成数组
用ASP+FSO生成JS文件
ASP动态生成的javascript表单验证代码
在asp中通过getrows实现数据库记录分页的一段代码
asp错误的几种处理方式
asp数组使用(2)
asp中 select top 问题!~
批量复制数据

ASP实例教程:TextStream对象


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 161 ::
收藏到网摘: n/a

TextStream对象

读文件

本例演示如何使用FileSystemObject的OpenTextFile方法来创建一个TextStream对象。TextStream对象的ReadAll方法会从已打开的文本文件中取得内容。

本文由软晨学习网(www.ruanchen.com)发布!转载和采集的话请不要去掉!谢谢。

本示例代码如下:

<html>
<body>
<p>这就是文本文件中的文本:</p>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("/example/aspe/testread.txt"), 1)
Response.Write(f.ReadAll)
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>

本实例运行结果如下:

这就是文本文件中的文本:
Hello! How are you today?

读文本文件中的一个部分

本例演示如何仅仅读取一个文本流文件的部分内容。

本示例代码如下:

<html>
<body>
<p>这是从文本文件中读取的前 5 个字符:</p>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
Response.Write(f.Read(5))
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>

本实例运行结果如下:

这是从文本文件中读取的前 5 个字符:
Hello

读文本文件中的一行

本例演示如何从一个文本流文件中读取一行内容。

本示例代码如下:

<html>
<body>
<p>这是从文本文件中读取的第一行:</p>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
Response.Write(f.ReadLine)
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>

本实例运行结果如下:

这是从文本文件中读取的第一行:
Hello!

读取文本文件的所有行

本例演示如何从文本流文件中读取所有的行。

本示例代码如下:

<html>
<body>
<p>这是从文本文件中读取的所有行:</p>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
do while f.AtEndOfStream = false
Response.Write(f.ReadLine)
Response.Write("<br>")
loop
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>

本实例运行结果如下:

这是从文本文件中读取的所有行:
Hello!
How are you today?

略过文本文件的一部分

本例演示如何在读取文本流文件时跳过指定的字符数。

本示例代码如下:

<html>
<body>
<p>文本文件中的前 4 个字符被略掉了:</p>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
f.Skip(4)
Response.Write(f.ReadAll)
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>

本实例运行结果如下:

文本文件中的前 4 个字符被略掉了:
o! How are you today?

略过文本文件的一行

本例演示如何在读取文本流文件时跳过一行。

本示例代码如下:

<html>
<body>
<p>文本文件中的第一行被略掉了:</p>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
f.SkipLine
Response.Write(f.ReadAll)
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>

本实例运行结果如下:

文本文件中的第一行被略掉了:
How are you today?

返回行数

本例演示如何返回在文本流文件中的当前行号。

本示例代码如下:

<html>
<body>
<p>这是文本文件中的所有行(带有行号):</p>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
do while f.AtEndOfStream = false
Response.Write("Line:" & f.Line & " ")
Response.Write(f.ReadLine)
Response.Write("<br>")
loop
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>

本实例运行结果如下:

这是文本文件中的所有行(带有行号):
Line:1 Hello!
Line:2 How are you today?

取得列数

本例演示如何取得在文件中当前字符的列号。

加此信息软晨学习网(www.ruanchen.com)发布目的是为了防止你变懒!ruanchen.com不主张采集!

本示例代码如下:

<html>
<body>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
Response.Write(f.Read(2))
Response.Write("<p>指针目前位于文本文件中的位置 " & f.Column & "。</p>")
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body>
</html>

本实例运行结果如下:

He
指针目前位于文本文件中的位置 3。