TextStream對(duì)象
讀文件
本例演示如何使用FileSystemObject的OpenTextFile方法來(lái)創(chuàng)建一個(gè)TextStream對(duì)象。TextStream對(duì)象的ReadAll方法會(huì)從已打開的文本文件中取得內(nèi)容。
本示例代碼如下:
<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>
本實(shí)例運(yùn)行結(jié)果如下:
這就是文本文件中的文本:
Hello! How are you today?
讀文本文件中的一個(gè)部分
本例演示如何僅僅讀取一個(gè)文本流文件的部分內(nèi)容。
本示例代碼如下:
<html>
<body>
<p>這是從文本文件中讀取的前 5 個(gè)字符:</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>
本實(shí)例運(yùn)行結(jié)果如下:
這是從文本文件中讀取的前 5 個(gè)字符:
Hello
讀文本文件中的一行
本例演示如何從一個(gè)文本流文件中讀取一行內(nèi)容。
本示例代碼如下:
<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>
本實(shí)例運(yùn)行結(jié)果如下:
這是從文本文件中讀取的第一行:
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>
本實(shí)例運(yùn)行結(jié)果如下:
這是從文本文件中讀取的所有行:
Hello!
How are you today?
略過(guò)文本文件的一部分
本例演示如何在讀取文本流文件時(shí)跳過(guò)指定的字符數(shù)。
本示例代碼如下:
<html>
<body>
<p>文本文件中的前 4 個(gè)字符被略掉了:</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>
本實(shí)例運(yùn)行結(jié)果如下:
文本文件中的前 4 個(gè)字符被略掉了:
o! How are you today?
略過(guò)文本文件的一行
本例演示如何在讀取文本流文件時(shí)跳過(guò)一行。
本示例代碼如下:
<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>
本實(shí)例運(yùn)行結(jié)果如下:
文本文件中的第一行被略掉了:
How are you today?
返回行數(shù)
本例演示如何返回在文本流文件中的當(dāng)前行號(hào)。
本示例代碼如下:
<html>
<body>
<p>這是文本文件中的所有行(帶有行號(hào)):</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>
本實(shí)例運(yùn)行結(jié)果如下:
這是文本文件中的所有行(帶有行號(hào)):
Line:1 Hello!
Line:2 How are you today?
取得列數(shù)
本例演示如何取得在文件中當(dāng)前字符的列號(hào)。
本示例代碼如下:
<html>
新聞熱點(diǎn)
疑難解答
圖片精選