FileSystem.OpenTextFieldParser 方法
定义
使用 OpenTextFieldParser 方法可以创建 TextFieldParser 对象,该对象为分析结构化文本文件(例如日志)提供了一种简单而有效的方法。The OpenTextFieldParser method allows you to create a TextFieldParser object, which provides a way to easily and efficiently parse structured text files, such as logs. TextFieldParser 对象可用于读取分隔文件和固定宽度文件。The TextFieldParser object can be used to read both delimited and fixed-width files.
重载
| OpenTextFieldParser(String, String[]) |
使用 |
| OpenTextFieldParser(String) |
使用 |
| OpenTextFieldParser(String, Int32[]) |
使用 |
OpenTextFieldParser(String, String[])
使用 OpenTextFieldParser 方法可以创建 TextFieldParser 对象,该对象为分析结构化文本文件(例如日志)提供了一种简单而有效的方法。The OpenTextFieldParser method allows you to create a TextFieldParser object, which provides a way to easily and efficiently parse structured text files, such as logs. TextFieldParser 对象可用于读取分隔文件和固定宽度文件。The TextFieldParser object can be used to read both delimited and fixed-width files.
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 打开的文件。The file to be opened with the TextFieldParser.
- delimiters
- String[]
字段分隔符。Delimiters for the fields.
返回
用于读取指定文件的 TextFieldParser。TextFieldParser to read the specified file.
例外
路径由于以下原因之一无效:是零长度字符串;仅为空白;包含无效字符;是一个设备路径(以 \\.\ 开头);以尾部反斜杠结尾。The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\); it ends with a trailing slash.
file 为 Nothing。file is Nothing.
文件不存在。The file does not exist.
文件正由另一个进程使用,或者出现 I/O 错误。The file is in use by another process, or an I/O error occurs.
路径超过了系统定义的最大长度。The path exceeds the system-defined maximum length.
路径中的文件名或目录名包含冒号 (:),或格式无效。A file or directory name in the path contains a colon (:) or is in an invalid format.
无法使用指定的格式分析某行 ()。A row cannot be parsed using the specified format. 此异常消息指定导致发生异常的行,同时将 ErrorLine 属性分配给该行中包含的文本。The exception message specifies the line causing the exception, while the ErrorLine property is assigned the text contained in the line.
该用户缺少查看该路径所必需的权限。The user lacks necessary permissions to view the path.
示例
此示例打开 TextFieldParser.reader,并使用它读取 C:\TestFolder1\Test1.txt。This example opens the TextFieldParser.reader and uses it to read from 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 。The following table lists examples of tasks involving the My.Computer.FileSystem.OpenTextFieldParser method.
| 功能To | 查看See |
|---|---|
| 从带分隔符的文本文件中读取Read from a delimited text file | 如何:在 Visual Basic 中读取逗号分隔的文本文件How to: Read From Comma-Delimited Text Files in Visual Basic |
| 从固定宽度文本文件中读取Read from a fixed-width text file | 如何:在 Visual Basic 中读取固定宽度的文本文件How to: Read From Fixed-width Text Files in Visual Basic |
| 从具有多种格式的文本文件中读取Read from a text file with multiple formats | 如何:在 Visual Basic 中读取具有多种格式的文本文件How to: Read From Text Files with Multiple Formats in Visual Basic |
另请参阅
适用于
OpenTextFieldParser(String)
使用 OpenTextFieldParser 方法可以创建 TextFieldParser 对象,该对象为分析结构化文本文件(例如日志)提供了一种简单而有效的方法。The OpenTextFieldParser method allows you to create a TextFieldParser object, which provides a way to easily and efficiently parse structured text files, such as logs. TextFieldParser 对象可用于读取分隔文件和固定宽度文件。The TextFieldParser object can be used to read both delimited and fixed-width files.
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 打开的文件。The file to be opened with the TextFieldParser.
返回
用于读取指定文件的 TextFieldParser。TextFieldParser to read the specified file.
例外
路径由于以下原因之一无效:是零长度字符串;仅为空白;包含无效字符;是一个设备路径(以 \\.\ 开头);以尾部反斜杠结尾。The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\); it ends with a trailing slash.
file 为 Nothing。file is Nothing.
文件不存在。The file does not exist.
文件正由另一个进程使用,或者出现 I/O 错误。The file is in use by another process, or an I/O error occurs.
路径超过了系统定义的最大长度。The path exceeds the system-defined maximum length.
路径中的文件名或目录名包含冒号 (:),或格式无效。A file or directory name in the path contains a colon (:) or is in an invalid format.
无法使用指定的格式分析某行 ()。A row cannot be parsed using the specified format. 此异常消息指定导致发生异常的行,同时将 ErrorLine 属性分配给该行中包含的文本。The exception message specifies the line causing the exception, while the ErrorLine property is assigned the text contained in the line.
该用户缺少查看该路径所必需的权限。The user lacks necessary permissions to view the path.
示例
此示例打开 TextFieldParser.reader,并使用它读取 C:\TestFolder1\Test1.txt。This example opens the TextFieldParser.reader and uses it to read from 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 。The following table lists examples of tasks involving the My.Computer.FileSystem.OpenTextFieldParser method.
| 功能To | 查看See |
|---|---|
| 从带分隔符的文本文件中读取Read from a delimited text file | 如何:在 Visual Basic 中读取逗号分隔的文本文件How to: Read From Comma-Delimited Text Files in Visual Basic |
| 从固定宽度文本文件中读取Read from a fixed-width text file | 如何:在 Visual Basic 中读取固定宽度的文本文件How to: Read From Fixed-width Text Files in Visual Basic |
| 从具有多种格式的文本文件中读取Read from a text file with multiple formats | 如何:在 Visual Basic 中读取具有多种格式的文本文件How to: Read From Text Files with Multiple Formats in Visual Basic |
另请参阅
适用于
OpenTextFieldParser(String, Int32[])
使用 OpenTextFieldParser 方法可以创建 TextFieldParser 对象,该对象为分析结构化文本文件(例如日志)提供了一种简单而有效的方法。The OpenTextFieldParser method allows you to create a TextFieldParser object, which provides a way to easily and efficiently parse structured text files, such as logs. TextFieldParser 对象可用于读取分隔文件和固定宽度文件。The TextFieldParser object can be used to read both delimited and fixed-width files.
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 打开的文件。The file to be opened with the TextFieldParser.
- fieldWidths
- Int32[]
字段的宽度。Widths of the fields.
返回
用于读取指定文件的 TextFieldParser。TextFieldParser to read the specified file.
例外
路径由于以下原因之一无效:是零长度字符串;仅为空白;包含无效字符;是一个设备路径(以 \\.\ 开头);以尾部反斜杠结尾。The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\); it ends with a trailing slash.
file 为 Nothing。file is Nothing.
文件不存在。The file does not exist.
文件正由另一个进程使用,或者出现 I/O 错误。The file is in use by another process, or an I/O error occurs.
路径超过了系统定义的最大长度。The path exceeds the system-defined maximum length.
路径中的文件名或目录名包含冒号 (:),或格式无效。A file or directory name in the path contains a colon (:) or is in an invalid format.
无法使用指定的格式分析某行 ()。A row cannot be parsed using the specified format. 此异常消息指定导致发生异常的行,同时将 ErrorLine 属性分配给该行中包含的文本。The exception message specifies the line causing the exception, while the ErrorLine property is assigned the text contained in the line.
该用户缺少查看该路径所必需的权限。The user lacks necessary permissions to view the path.
示例
此示例打开 TextFieldParser.reader,并使用它读取 C:\TestFolder1\Test1.txt。This example opens the TextFieldParser.reader and uses it to read from 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 。The following table lists examples of tasks involving the My.Computer.FileSystem.OpenTextFieldParser method.
| 功能To | 查看See |
|---|---|
| 从带分隔符的文本文件中读取Read from a delimited text file | 如何:在 Visual Basic 中读取逗号分隔的文本文件How to: Read From Comma-Delimited Text Files in Visual Basic |
| 从固定宽度文本文件中读取Read from a fixed-width text file | 如何:在 Visual Basic 中读取固定宽度的文本文件How to: Read From Fixed-width Text Files in Visual Basic |
| 从具有多种格式的文本文件中读取Read from a text file with multiple formats | 如何:在 Visual Basic 中读取具有多种格式的文本文件How to: Read From Text Files with Multiple Formats in Visual Basic |