日期:2008-07-15  浏览次数:20717 次

Aiyiweb.Com提示:读文件:本例演示如何使用FileSystemObject的OpenTextFile方法来创建一个TextStream对象。TextStream对象的ReadAll方法会从已打开的文本文件中取得内容。读文本文件中的一个部分:本例演示如何仅仅读取一个文本流文件的部分内容。读文本文件中的一行:本例演示如何从一个文

TextStream对象

读文件

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

本文由爱易网任务室(www.Aiyiweb.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!

读取文本文件的所有行

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

本文是爱易网(http://www.Aiyiweb.Com)收集整理或者原创内容,转载请注明出处!

本示例代码如下:

<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?

前往行数

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

本示例代