TextFieldParser 類別

定義

提供用於剖析結構化文字檔的方法和屬性。

public ref class TextFieldParser : IDisposable
public class TextFieldParser : IDisposable
type TextFieldParser = class
    interface IDisposable
Public Class TextFieldParser
Implements IDisposable
繼承
TextFieldParser
實作

範例

這個範例會剖析以製表元分隔的文字檔案 Bigfile

Using MyReader As New Microsoft.VisualBasic.FileIO.
    TextFieldParser("c:\logs\bigfile")

    MyReader.TextFieldType = 
        Microsoft.VisualBasic.FileIO.FieldType.Delimited
    MyReader.Delimiters = New String() {vbTab}
    Dim currentRow As String()
    'Loop through all of the fields in the file. 
    'If any lines are corrupt, report an error and continue parsing. 
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadFields()
            ' Include code here to handle the row.
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & 
            " is invalid.  Skipping")
        End Try
    End While
End Using

備註

物件 TextFieldParser 提供剖析結構化文本檔的方法和屬性。 剖析具有 TextFieldParser 的文本文件類似於逐一查看文本檔,而使用 ReadFields 方法來擷取文字字段類似於分割字串。

TextFieldParser可以剖析兩種類型的檔案:分隔或固定寬度。 某些屬性,例如 DelimitersHasFieldsEnclosedInQuotes 只有在使用分隔檔案時才有意義,而 FieldWidths 屬性只有在使用固定寬度檔案時才有意義。

下表列出與 Microsoft.VisualBasic.FileIO.TextFieldParser 物件相關的工作範例。

收件者 請參閱
從分隔的文字檔讀取 作法:從逗號分隔文字檔讀取
從固定寬度文本檔讀取 作法:從固定寬度的文字檔讀取
從具有多種格式的文字檔讀取 作法:以多種格式從文字檔讀取

建構函式

TextFieldParser(Stream)

初始化 TextFieldParser 類別的新執行個體。

TextFieldParser(Stream, Encoding)

初始化 TextFieldParser 類別的新執行個體。

TextFieldParser(Stream, Encoding, Boolean)

初始化 TextFieldParser 類別的新執行個體。

TextFieldParser(Stream, Encoding, Boolean, Boolean)

初始化 TextFieldParser 類別的新執行個體。

TextFieldParser(String)

初始化 TextFieldParser 類別的新執行個體。

TextFieldParser(String, Encoding)

初始化 TextFieldParser 類別的新執行個體。

TextFieldParser(String, Encoding, Boolean)

初始化 TextFieldParser 類別的新執行個體。

TextFieldParser(TextReader)

初始化 TextFieldParser 類別的新執行個體。

屬性

CommentTokens

定義註解語彙基元 (Token)。 註解語彙基元是一個字串,當置於一行的開頭時,表示該行為註解,剖析器 (Parser) 應當忽略。

Delimiters

定義文字檔的分隔符號。

EndOfData

如果目前的游標位置和檔案結尾之間沒有非空白的非註解行,則會傳回 True

ErrorLine

傳回造成最新 MalformedLineException 例外狀況的行。

ErrorLineNumber

傳回造成最新 MalformedLineException 例外狀況的行號。

FieldWidths

代表正在剖析文字檔中每一個資料行的寬度。

HasFieldsEnclosedInQuotes

代表分隔的檔案進行剖析時,欄位是否加上引號。

LineNumber

傳回目前的行號,如果資料流中已沒有可用的字元,則會傳回 -1。

TextFieldType

指出剖析的檔案是分隔的檔案,還是固定寬度的檔案。

TrimWhiteSpace

指出是否應當去除欄位值中前置和結尾的空白字元。

方法

Close()

關閉目前的 TextFieldParser 物件。

Dispose()

釋放由 TextFieldParser 物件使用的資源。

Dispose(Boolean)

釋放由 TextFieldParser 物件使用的資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Finalize()

在記憶體回收 (GC) 回收 TextFieldParser 物件前,允許其嘗試釋放資源並執行其他清除作業。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
PeekChars(Int32)

讀取指定數目的字元而不使游標前進。

ReadFields()

讀取目前行上的所有欄位,將它們傳回為字串陣列,並將游標前進至包含資料的下一行。

ReadLine()

將目前的行以字串傳回,並將游標前進至下一行。

ReadToEnd()

讀取文字檔的其餘部分,並將其以字串傳回。

SetDelimiters(String[])

將讀取器 (Reader) 的分隔符號設為指定的值,並將欄位型別 (Field Type) 設為 Delimited

SetFieldWidths(Int32[])

將讀取器 (Reader) 的分隔符號設定為指定的值。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

IDisposable.Dispose()

釋放由 TextFieldParser 物件使用的資源。

適用於

另請參閱