ASP TextStream

TextStream 的读写方法、逐行处理、编码与位置控制。

Intermediate
Key PointsTextStream 是文本文件的读写句柄:ReadLine 逐行读,WriteLine 逐行写,AtEndOfStream 判断结束。

属性与方法

MemberPurpose
Read(n)按字符读
ReadLine不含换行符
ReadAll整个文件
Skip(n)跳过 n 个字符
SkipLine
Write(s)不换行
WriteLine(s)自动加换行
WriteBlankLines(n)n 个换行
AtEndOfStream读到末尾为 True
Line / Column当前行列

逐行读取

ASP
<%
    Dim fso, ts, line, n
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    Set ts = fso.OpenTextFile(Server.MapPath("/data/users.txt"), 1)

    n = 0
    Do While Not ts.AtEndOfStream
        line = ts.ReadLine
        n = n + 1
        Response.Write n & ": " & Server.HTMLEncode(line) & "<br>"
    Loop
    ts.Close
    Set ts = Nothing
    Set fso = Nothing
%>
Output
1: Ada,ada@example.com
2: Bob,bob@example.com
3: Cy,cy@example.com

Use Do While Not ts.AtEndOfStream rather than a fixed count: the number of lines is unknown, and a hardcoded loop either truncates or reads empty lines.

逐行写入

ASP
<%
    Dim rows, i
    rows = Array("Ada,ada@example.com", "Bob,bob@example.com")

    Set ts = fso.CreateTextFile(Server.MapPath("/data/users.txt"), True)
    For i = 0 To UBound(rows)
        ts.WriteLine rows(i)
    Next
    ts.Close
%>

The second argument True means overwrite if present. With False an existing file raises an error — an easy omission that produces a "file already exists" runtime error.

解析 CSV

ASP
<%
    Dim parts
    Do While Not ts.AtEndOfStream
        parts = Split(ts.ReadLine, ",")
        If UBound(parts) >= 1 Then
            Response.Write "<tr><td>" & Server.HTMLEncode(parts(0)) & _
                           "</td><td>" & Server.HTMLEncode(parts(1)) & "</td></tr>"
        End If
    Loop
%>

Split handles simple CSV but breaks on fields containing commas or quoted escapes. Real CSV needs a quote-aware parser; Split splits "Ada, Jr." into two fields.

编码

ASP
<%
    ' 默认按 ASCII 读写,中文会变问号
    Set ts = fso.CreateTextFile(path, True, True)      ' 第三参数 True = Unicode(UTF-16)
    Set ts = fso.OpenTextFile(path, 1, False, -1)      ' 第四参数 -1 = Unicode
    Set ts = fso.OpenTextFile(path, 1, False, 0)       ' 0 = ASCII(默认)
    Set ts = fso.OpenTextFile(path, 1, False, -2)      ' -2 = 系统默认编码
%>

The encoding argument only offers ASCII, Unicode (UTF-16) and system default — no UTF-8. For UTF-8 files use ADODB.Stream with Charset = "utf-8".

ASP
<%
    Dim st
    Set st = Server.CreateObject("ADODB.Stream")
    st.Type = 2                       ' 文本
    st.Charset = "utf-8"
    st.Open
    st.LoadFromFile Server.MapPath("/data/utf8.txt")
    Response.Write st.ReadText
    st.Close
    Set st = Nothing
%>
TextStream suits small files. ReadAll on a tens-of-MB log loads it all into memory and spikes the IIS worker. Use ADODB.Stream chunked reads or a log tool for large files.

锁文件

ASP
<%
    ' 简单的互斥:用文件存在与否当锁
    If Not fso.FileExists(lockPath) Then
        Set ts = fso.CreateTextFile(lockPath, True)
        ts.Write "locked"
        ts.Close
        ' 做需要互斥的事
        fso.DeleteFile lockPath
    End If
%>

This file-lock has a race: two requests can both see "no file" and both create it. For real mutual exclusion use Application.Lock or a database row lock, not the filesystem.

Friends & Partners