ASP实例教程:TextStream对象


TextStream对象

读文件

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

本文由网页教学网(www.phpstudy.net)发布!转载和采集的话请不要去掉!谢谢。

本示例代码如下:

<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.phpstudy.net)收集整理或者原创内容,转载请注明出处!

本示例代码如下:

<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.phpstudy.net)发布目的是为了防止你变懒!phpstudy.net不主张采集!

本示例代码如下:

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


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3