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 物件的任務範例。

看!
從分隔的文字檔讀取 如何讀取 Comma-Delimited 文本文件
從固定寬度文字檔讀取 如何:從固定寬度文本文件讀取
從多種格式的文字檔讀取 如何:從具有多種格式的文本文件讀取

建構函式

名稱 Description
TextFieldParser(Stream, Encoding, Boolean, Boolean)

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

TextFieldParser(Stream, Encoding, Boolean)

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

TextFieldParser(Stream, Encoding)

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

TextFieldParser(Stream)

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

TextFieldParser(String, Encoding, Boolean)

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

TextFieldParser(String, Encoding)

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

TextFieldParser(String)

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

TextFieldParser(TextReader)

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

屬性

名稱 Description
CommentTokens

定義留言標記。 註解標記是一串字串,放在行首表示該行是註解,解析器應忽略它。

Delimiters

定義了文字檔案的分隔符。

EndOfData

如果當前游標位置與檔案末尾之間沒有非空白、非註解的行,則回傳 True

ErrorLine

回傳導致最近 MalformedLineException 異常的那一行。

ErrorLineNumber

回傳造成最近 MalformedLineException 一次例外的行號。

FieldWidths

表示正在解析的文本檔案中每一欄的寬度。

HasFieldsEnclosedInQuotes

表示解析分隔檔案時欄位是否以引號包圍。

LineNumber

回傳目前的行號,或 -1 如果串流中沒有更多字元可用,則返回。

TextFieldType

指示要解析的檔案是分隔還是固定寬度。

TrimWhiteSpace

指示是否應將前置與後方的空白區從場值中裁掉。

方法

名稱 Description
Close()

關閉當前 TextFieldParser 物件。

Dispose()

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

Dispose(Boolean)

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

Equals(Object)

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

(繼承來源 Object)
Finalize()

允許物件 TextFieldParser 嘗試釋放資源並執行其他清理操作,然後再被垃圾回收回收。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
PeekChars(Int32)

讀取指定字元數且不推進游標。

ReadFields()

讀取當前行上的所有欄位,以字串陣列的形式回傳,並將游標推進到下一行包含資料的行。

ReadLine()

以字串回傳當前行,並將游標移至下一行。

ReadToEnd()

讀取剩餘的文字檔,並以字串形式回傳。

SetDelimiters(String[])

將讀取器的分隔符設為指定值,並將欄位類型設為 Delimited

SetFieldWidths(Int32[])

將讀取器的分隔符設定為指定的值。

ToString()

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

(繼承來源 Object)

適用於

另請參閱