Share via


TextFieldParser 物件

更新:2007 年 11 月

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

Public Class TextFieldParser

例外狀況

下列情況可能會導致例外狀況:

  • 文字欄位不符指定的格式。例如,在固定寬度檔案中,其中一個欄位不符指定的寬度。(MalformedLineException)

備註

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

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

工作

下表列出包含 Microsoft.VisualBasic.FileIO.TextFieldParser 物件的工作範例。

若要

請參閱

讀取分隔的文字檔

HOW TO:在 Visual Basic 中從逗號分隔文字檔讀取

讀取固定寬度的文字檔

HOW TO:在 Visual Basic 中從固定寬度的文字檔讀取

以多種格式讀取文字檔

HOW TO:在 Visual Basic 中以多種格式從文字檔讀取

範例

這個範例剖析定位分隔文字檔 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

這個範例是根據是否有 (processFields) 函式來執行的,該函式會在讀取欄位時進行處理。

需求

命名空間Microsoft.VisualBasic.FileIO

類別︰TextFieldParser

組件:Visual Basic Runtime Library (在 Microsoft.VisualBasic.dll 中)

請參閱

工作

疑難排解例外狀況:Microsoft.VisualBasic.FileIO.TextFieldParser.MalformedLineException

概念

使用 TextFieldParser 物件剖析文字檔

參考

TextFieldParser 物件成員

TextFieldParser.CommentTokens 屬性

TextFieldParser.Delimiters 屬性

TextFieldParser.EndOfData 屬性

TextFieldParser.ErrorLine 屬性

TextFieldParser.ErrorLineNumber 屬性

TextFieldParser.FieldWidths 屬性

TextFieldParser.HasFieldsEnclosedInQuotes 屬性

TextFieldParser.LineNumber 屬性

TextFieldParser.TextFieldType 屬性

TextFieldParser.TrimWhiteSpace 屬性

TextFieldParser.Close 方法

TextFieldParser.PeekChars 方法

TextFieldParser.ReadFields 方法

TextFieldParser.ReadLine 方法

TextFieldParser.ReadToEnd 方法

TextFieldParser.SetDelimiters 方法

TextFieldParser.SetFieldWidths 方法

My.Computer.FileSystem.OpenTextFieldParser 方法

Microsoft.VisualBasic.FileIO.TextFieldParser