FileSystem.OpenTextFieldParser 方法

定義

這個 OpenTextFieldParser 方法允許你建立 TextFieldParser 一個物件,提供一種輕鬆且有效率地解析結構化文字檔(如日誌)的方法。 這個 TextFieldParser 物件可以用來讀取分隔與固定寬度的檔案。

多載

名稱 Description
OpenTextFieldParser(String, String[])

這個 OpenTextFieldParser 方法允許你建立 TextFieldParser 一個物件,提供一種輕鬆且有效率地解析結構化文字檔(如日誌)的方法。 這個 TextFieldParser 物件可以用來讀取分隔與固定寬度的檔案。

OpenTextFieldParser(String)

這個 OpenTextFieldParser 方法允許你建立 TextFieldParser 一個物件,提供一種輕鬆且有效率地解析結構化文字檔(如日誌)的方法。 這個 TextFieldParser 物件可以用來讀取分隔與固定寬度的檔案。

OpenTextFieldParser(String, Int32[])

這個 OpenTextFieldParser 方法允許你建立 TextFieldParser 一個物件,提供一種輕鬆且有效率地解析結構化文字檔(如日誌)的方法。 這個 TextFieldParser 物件可以用來讀取分隔與固定寬度的檔案。

OpenTextFieldParser(String, String[])

這個 OpenTextFieldParser 方法允許你建立 TextFieldParser 一個物件,提供一種輕鬆且有效率地解析結構化文字檔(如日誌)的方法。 這個 TextFieldParser 物件可以用來讀取分隔與固定寬度的檔案。

public:
 static Microsoft::VisualBasic::FileIO::TextFieldParser ^ OpenTextFieldParser(System::String ^ file, ... cli::array <System::String ^> ^ delimiters);
public static Microsoft.VisualBasic.FileIO.TextFieldParser OpenTextFieldParser(string file, params string[] delimiters);
static member OpenTextFieldParser : string * string[] -> Microsoft.VisualBasic.FileIO.TextFieldParser
Public Shared Function OpenTextFieldParser (file As String, ParamArray delimiters As String()) As TextFieldParser

參數

file
String

要用 TextFieldParser. 開啟的檔案。

delimiters
String[]

欄位的分隔符。

傳回

TextFieldParser 讀取指定的檔案。

例外狀況

該路徑不成立,原因之一是:它是零長度字串;它僅包含空白;它包含無效字元;或是裝置路徑(以 \\.\ 開頭);結尾是拖尾的斜線。

fileNothing

檔案不存在。

該檔案被其他程序使用,或發生 I/O 錯誤。

路徑超過系統定義的最大長度。

路徑中的檔案或目錄名稱包含冒號(:),或格式無效。

一列無法用指定格式解析。 例外狀況訊息會指定造成例外狀況的行,而 ErrorLine 屬性會指派該行中包含的文字。

使用者缺乏必要的權限來查看路徑。

範例

此範例開啟 並 TextFieldParser.reader 用它讀取 C:\TestFolder1\Test1.txt

Dim reader = My.Computer.FileSystem.OpenTextFieldParser(
    "C:\TestFolder1\test1.txt")
reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
reader.delimiters = {","}
Dim currentRow As String()
While Not reader.EndOfData
    Try
        currentRow = reader.ReadFields()
        Dim currentField As String
        For Each currentField In currentRow
            MsgBox(currentField)
        Next
    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
        MsgBox("Line " & ex.Message &
        "is not valid and will be skipped.")
    End Try
End While

備註

下表列出與此 My.Computer.FileSystem.OpenTextFieldParser 方法相關的任務範例。

看!
從分隔的文字檔讀取 如何閱讀Visual Basic
從固定寬度文字檔讀取 如何閱讀Visual Basic
從多種格式的文字檔讀取 如何閱讀具有多種格式的 Visual Basic

另請參閱

適用於

OpenTextFieldParser(String)

這個 OpenTextFieldParser 方法允許你建立 TextFieldParser 一個物件,提供一種輕鬆且有效率地解析結構化文字檔(如日誌)的方法。 這個 TextFieldParser 物件可以用來讀取分隔與固定寬度的檔案。

public:
 static Microsoft::VisualBasic::FileIO::TextFieldParser ^ OpenTextFieldParser(System::String ^ file);
public static Microsoft.VisualBasic.FileIO.TextFieldParser OpenTextFieldParser(string file);
static member OpenTextFieldParser : string -> Microsoft.VisualBasic.FileIO.TextFieldParser
Public Shared Function OpenTextFieldParser (file As String) As TextFieldParser

參數

file
String

要用 TextFieldParser. 開啟的檔案。

傳回

TextFieldParser 讀取指定的檔案。

例外狀況

該路徑不成立,原因之一是:它是零長度字串;它僅包含空白;它包含無效字元;或是裝置路徑(以 \\.\ 開頭);結尾是拖尾的斜線。

fileNothing

檔案不存在。

該檔案被其他程序使用,或發生 I/O 錯誤。

路徑超過系統定義的最大長度。

路徑中的檔案或目錄名稱包含冒號(:),或格式無效。

一列無法用指定格式解析。 例外狀況訊息會指定造成例外狀況的行,而 ErrorLine 屬性會指派該行中包含的文字。

使用者缺乏必要的權限來查看路徑。

範例

此範例開啟 並 TextFieldParser.reader 用它讀取 C:\TestFolder1\Test1.txt

Dim reader = My.Computer.FileSystem.OpenTextFieldParser(
    "C:\TestFolder1\test1.txt")
reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
reader.delimiters = {","}
Dim currentRow As String()
While Not reader.EndOfData
    Try
        currentRow = reader.ReadFields()
        Dim currentField As String
        For Each currentField In currentRow
            MsgBox(currentField)
        Next
    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
        MsgBox("Line " & ex.Message &
        "is not valid and will be skipped.")
    End Try
End While

備註

下表列出與此 My.Computer.FileSystem.OpenTextFieldParser 方法相關的任務範例。

看!
從分隔的文字檔讀取 如何閱讀Visual Basic
從固定寬度文字檔讀取 如何閱讀Visual Basic
從多種格式的文字檔讀取 如何閱讀具有多種格式的 Visual Basic

另請參閱

適用於

OpenTextFieldParser(String, Int32[])

這個 OpenTextFieldParser 方法允許你建立 TextFieldParser 一個物件,提供一種輕鬆且有效率地解析結構化文字檔(如日誌)的方法。 這個 TextFieldParser 物件可以用來讀取分隔與固定寬度的檔案。

public:
 static Microsoft::VisualBasic::FileIO::TextFieldParser ^ OpenTextFieldParser(System::String ^ file, ... cli::array <int> ^ fieldWidths);
public static Microsoft.VisualBasic.FileIO.TextFieldParser OpenTextFieldParser(string file, params int[] fieldWidths);
static member OpenTextFieldParser : string * int[] -> Microsoft.VisualBasic.FileIO.TextFieldParser
Public Shared Function OpenTextFieldParser (file As String, ParamArray fieldWidths As Integer()) As TextFieldParser

參數

file
String

要用 TextFieldParser. 開啟的檔案。

fieldWidths
Int32[]

田的寬度。

傳回

TextFieldParser 讀取指定的檔案。

例外狀況

該路徑不成立,原因之一是:它是零長度字串;它僅包含空白;它包含無效字元;或是裝置路徑(以 \\.\ 開頭);結尾是拖尾的斜線。

fileNothing

檔案不存在。

該檔案被其他程序使用,或發生 I/O 錯誤。

路徑超過系統定義的最大長度。

路徑中的檔案或目錄名稱包含冒號(:),或格式無效。

一列無法用指定格式解析。 例外狀況訊息會指定造成例外狀況的行,而 ErrorLine 屬性會指派該行中包含的文字。

使用者缺乏必要的權限來查看路徑。

範例

此範例開啟 並 TextFieldParser.reader 用它讀取 C:\TestFolder1\Test1.txt

Dim reader = My.Computer.FileSystem.OpenTextFieldParser(
    "C:\TestFolder1\test1.txt")
reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
reader.delimiters = {","}
Dim currentRow As String()
While Not reader.EndOfData
    Try
        currentRow = reader.ReadFields()
        Dim currentField As String
        For Each currentField In currentRow
            MsgBox(currentField)
        Next
    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
        MsgBox("Line " & ex.Message &
        "is not valid and will be skipped.")
    End Try
End While

備註

下表列出與此 My.Computer.FileSystem.OpenTextFieldParser 方法相關的任務範例。

看!
從分隔的文字檔讀取 如何閱讀Visual Basic
從固定寬度文字檔讀取 如何閱讀Visual Basic
從多種格式的文字檔讀取 如何閱讀具有多種格式的 Visual Basic

另請參閱

適用於