ASP TextStream 对象


TextStream 对象用于访问文本文件的内容。

TextStream 对象

TextStream 对象用于访问文本文件的内容。

下面的代码会创建一个文本文件 (c:\test.txt),然后向此文件写一些文本(变量 f 是 TextStream 对象的一个实例):

  1. <%
    dim fs,f
    set fs=Server.CreateObject("Scripting.FileSystemObject")
    set f=fs.CreateTextFile("c:\test.txt",true)
    f.WriteLine("Hello World!")
    f.Close
    set f=nothing
    set fs=nothing
    %>

如需创建 TextStream 对象的一个实例,您可以使用 FileSystemObject 对象的 CreateTextFile 方法或者 OpenTextFile 方法,也可以使用 File 对象的 OpenAsTextStream 方法。

TextStream 对象的属性和方法描述如下:

属性

属性描述
AtEndOfLine如果文件指针正好位于 TextStream 文件中行尾标记的前面,则该属性值返回 True;否则返回 False。
AtEndOfStream如果文件指针在 TextStream 文件末尾,则该属性值返回 True;否则返回 False。
Column返回 TextStream 文件输入流中的当前字符位置的列号。
Line返回 TextStream 文件中的当前行号。

方法

方法描述
Close关闭一个打开的 TextStream 文件。
Read从一个 TextStream 文件中读取指定数量的字符并返回结果。
ReadAll读取整个 TextStream 文件并返回结果。
ReadLine从一个 TextStream 文件读取一整行(到换行符但不包括换行符)并返回结果。
Skip当读取一个 TextStream 文件时跳过指定数量的字符。
SkipLine当读取一个 TextStream 文件时跳过下一行。
Write写入指定的文本到一个 TextStream 文件中。
WriteLine写入指定的文本和换行符到一个 TextStream 文件中。
WriteBlankLines写入指定数量的换行符到一个 TextStream 文件中。

在线实例

读取文本文件
本例演示如何从文本文件中读取内容。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>This is the text in the text file:</p>
  5. <%
  6. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  7.  
  8. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  9. Response.Write(f.ReadAll)
  10. f.Close
  11.  
  12. Set f=Nothing
  13. Set fs=Nothing
  14. %>
  15. </body>
  16. </html>

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

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>This is the first five characters from the text file:</p>
  5.  
  6. <%
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8.  
  9. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  10. Response.Write(f.Read(5))
  11. f.Close
  12.  
  13. Set f=Nothing
  14. Set fs=Nothing
  15. %>
  16.  
  17. </body>
  18. </html>

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

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>This is the first line of the text file:</p>
  5.  
  6. <%
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8.  
  9. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  10. Response.Write(f.ReadLine)
  11. f.Close
  12.  
  13. Set f=Nothing
  14. Set fs=Nothing
  15. %>
  16.  
  17. </body>
  18. </html>

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

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>This is all the lines in the text file:</p>
  5.  
  6. <%
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  9.  
  10. do while f.AtEndOfStream = false
  11. Response.Write(f.ReadLine)
  12. Response.Write("<br>")
  13. loop
  14.  
  15. f.Close
  16. Set f=Nothing
  17. Set fs=Nothing
  18. %>
  19.  
  20. </body>
  21. </html>

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

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>The first four characters in the text file are skipped:</p>
  5.  
  6. <%
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8.  
  9. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  10. f.Skip(4)
  11. Response.Write(f.ReadAll)
  12. f.Close
  13.  
  14. Set f=Nothing
  15. Set fs=Nothing
  16. %>
  17.  
  18. </body>
  19. </html>

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

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>The first line in the text file is skipped:</p>
  5.  
  6. <%
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8.  
  9. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  10. f.SkipLine
  11. Response.Write(f.ReadAll)
  12. f.Close
  13.  
  14. Set f=Nothing
  15. Set fs=Nothing
  16. %>
  17.  
  18. </body>
  19. </html>

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

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>This is all the lines in the text file (with line numbers):</p>
  5.  
  6. <%
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  9.  
  10. do while f.AtEndOfStream = false
  11. Response.Write("Line:" & f.Line & " ")
  12. Response.Write(f.ReadLine)
  13. Response.Write("<br>")
  14. loop
  15.  
  16. f.Close
  17. Set f=Nothing
  18. Set fs=Nothing
  19. %>
  20.  
  21. </body>
  22. </html>

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

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  7.  
  8. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  9. Response.Write(f.Read(2))
  10. Response.Write("<p>The cursor is now standing in position " & f.Column & " in the text file.</p>")
  11. f.Close
  12.  
  13. Set f=Nothing
  14. Set fs=Nothing
  15. %>
  16.  
  17. </body>
  18. </html>