TextFieldParser TextFieldParser TextFieldParser TextFieldParser Class

定義

提供用於剖析結構化文字檔的方法和屬性。Provides methods and properties for parsing structured text files.

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

範例

此範例會剖析 tab 鍵分隔的文字檔,透過BigfileThis example parses through a tab-delimited text file, 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物件會提供方法和屬性剖析結構化文字檔。The TextFieldParser object provides methods and properties for parsing structured text files. 剖析文字檔TextFieldParser類似於逐一查看文字檔案,而ReadFields方法來擷取文字欄位是類似於分割的字串。Parsing a text file with the TextFieldParser is similar to iterating over a text file, while the ReadFields method to extract fields of text is similar to splitting the strings.

TextFieldParser可以剖析兩種檔案類型: 分隔或固定寬度。The TextFieldParser can parse two types of files: delimited or fixed-width. 某些屬性,例如DelimitersHasFieldsEnclosedInQuotes才有意義時使用分隔的檔案,而FieldWidths屬性使用固定寬度檔案時,才有意義。Some properties, such as Delimiters and HasFieldsEnclosedInQuotes are meaningful only when working with delimited files, while the FieldWidths property is meaningful only when working with fixed-width files.

下表列出與工作的範例Microsoft.VisualBasic.FileIO.TextFieldParser物件。The following table lists examples of tasks involving the Microsoft.VisualBasic.FileIO.TextFieldParser object.

To 請參閱See
從分隔的文字檔讀取Read from a delimited text file 如何:從逗號分隔文字檔讀取How to: Read From Comma-Delimited Text Files
從固定寬度的文字檔讀取Read from a fixed-width text file 如何:從固定寬度的文字檔讀取How to: Read From Fixed-width Text Files
從以多種格式的文字檔讀取Read from a text file with multiple formats 如何:以多種格式從文字檔讀取How to: Read From Text Files with Multiple Formats

建構函式

TextFieldParser(Stream) TextFieldParser(Stream) TextFieldParser(Stream) TextFieldParser(Stream)

初始化 TextFieldParser 類別的新執行個體。Initializes a new instance of the TextFieldParser class.

TextFieldParser(Stream, Encoding) TextFieldParser(Stream, Encoding) TextFieldParser(Stream, Encoding) TextFieldParser(Stream, Encoding)

初始化 TextFieldParser 類別的新執行個體。Initializes a new instance of the TextFieldParser class.

TextFieldParser(Stream, Encoding, Boolean) TextFieldParser(Stream, Encoding, Boolean) TextFieldParser(Stream, Encoding, Boolean) TextFieldParser(Stream, Encoding, Boolean)

初始化 TextFieldParser 類別的新執行個體。Initializes a new instance of the TextFieldParser class.

TextFieldParser(Stream, Encoding, Boolean, Boolean) TextFieldParser(Stream, Encoding, Boolean, Boolean) TextFieldParser(Stream, Encoding, Boolean, Boolean) TextFieldParser(Stream, Encoding, Boolean, Boolean)

初始化 TextFieldParser 類別的新執行個體。Initializes a new instance of the TextFieldParser class.

TextFieldParser(String) TextFieldParser(String) TextFieldParser(String) TextFieldParser(String)

初始化 TextFieldParser 類別的新執行個體。Initializes a new instance of the TextFieldParser class.

TextFieldParser(String, Encoding) TextFieldParser(String, Encoding) TextFieldParser(String, Encoding) TextFieldParser(String, Encoding)

初始化 TextFieldParser 類別的新執行個體。Initializes a new instance of the TextFieldParser class.

TextFieldParser(String, Encoding, Boolean) TextFieldParser(String, Encoding, Boolean) TextFieldParser(String, Encoding, Boolean) TextFieldParser(String, Encoding, Boolean)

初始化 TextFieldParser 類別的新執行個體。Initializes a new instance of the TextFieldParser class.

TextFieldParser(TextReader) TextFieldParser(TextReader) TextFieldParser(TextReader) TextFieldParser(TextReader)

初始化 TextFieldParser 類別的新執行個體。Initializes a new instance of the TextFieldParser class.

屬性

CommentTokens CommentTokens CommentTokens CommentTokens

定義註解語彙基元 (Token)。Defines comment tokens. 註解語彙基元是一個字串,當置於一行的開頭時,表示該行為註解,剖析器 (Parser) 應當忽略。A comment token is a string that, when placed at the beginning of a line, indicates that the line is a comment and should be ignored by the parser.

Delimiters Delimiters Delimiters Delimiters

定義文字檔的分隔符號。Defines the delimiters for a text file.

EndOfData EndOfData EndOfData EndOfData

如果目前的游標位置和檔案結尾之間沒有非空白的非註解行,則會傳回 TrueReturns True if there are no non-blank, non-comment lines between the current cursor position and the end of the file.

ErrorLine ErrorLine ErrorLine ErrorLine

傳回造成最新 MalformedLineException 例外狀況的行。Returns the line that caused the most recent MalformedLineException exception.

ErrorLineNumber ErrorLineNumber ErrorLineNumber ErrorLineNumber

傳回造成最新 MalformedLineException 例外狀況的行號。Returns the number of the line that caused the most recent MalformedLineException exception.

FieldWidths FieldWidths FieldWidths FieldWidths

代表正在剖析文字檔中每一個資料行的寬度。Denotes the width of each column in the text file being parsed.

HasFieldsEnclosedInQuotes HasFieldsEnclosedInQuotes HasFieldsEnclosedInQuotes HasFieldsEnclosedInQuotes

代表分隔的檔案進行剖析時,欄位是否加上引號。Denotes whether fields are enclosed in quotation marks when a delimited file is being parsed.

LineNumber LineNumber LineNumber LineNumber

傳回目前的行號,如果資料流中已沒有可用的字元,則會傳回 -1。Returns the current line number, or returns -1 if no more characters are available in the stream.

TextFieldType TextFieldType TextFieldType TextFieldType

指出剖析的檔案是分隔的檔案,還是固定寬度的檔案。Indicates whether the file to be parsed is delimited or fixed-width.

TrimWhiteSpace TrimWhiteSpace TrimWhiteSpace TrimWhiteSpace

指出是否應當去除欄位值中前置和結尾的空白字元。Indicates whether leading and trailing white space should be trimmed from field values.

方法

Close() Close() Close() Close()

關閉目前的 TextFieldParser 物件。Closes the current TextFieldParser object.

Dispose() Dispose() Dispose() Dispose()

釋放由 TextFieldParser 物件使用的資源。Releases resources used by the TextFieldParser object.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

釋放由 TextFieldParser 物件使用的資源。Releases resources used by the TextFieldParser object.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize() Finalize() Finalize() Finalize()

在記憶體回收 (GC) 回收 TextFieldParser 物件前,允許其嘗試釋放資源並執行其他清除作業。Allows the TextFieldParser object to attempt to free resources and perform other cleanup operations before it is reclaimed by garbage collection.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
PeekChars(Int32) PeekChars(Int32) PeekChars(Int32) PeekChars(Int32)

讀取指定數目的字元而不使游標前進。Reads the specified number of characters without advancing the cursor.

ReadFields() ReadFields() ReadFields() ReadFields()

讀取目前行上的所有欄位,將它們傳回為字串陣列,並將游標前進至包含資料的下一行。Reads all fields on the current line, returns them as an array of strings, and advances the cursor to the next line containing data.

ReadLine() ReadLine() ReadLine() ReadLine()

將目前的行以字串傳回,並將游標前進至下一行。Returns the current line as a string and advances the cursor to the next line.

ReadToEnd() ReadToEnd() ReadToEnd() ReadToEnd()

讀取文字檔的其餘部分,並將其以字串傳回。Reads the remainder of the text file and returns it as a string.

SetDelimiters(String[]) SetDelimiters(String[]) SetDelimiters(String[]) SetDelimiters(String[])

將讀取器 (Reader) 的分隔符號設為指定的值,並將欄位型別 (Field Type) 設為 DelimitedSets the delimiters for the reader to the specified values, and sets the field type to Delimited.

SetFieldWidths(Int32[]) SetFieldWidths(Int32[]) SetFieldWidths(Int32[]) SetFieldWidths(Int32[])

將讀取器 (Reader) 的分隔符號設定為指定的值。Sets the delimiters for the reader to the specified values.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

適用於

另請參閱