TextFieldParser.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.
public:
cli::array <System::String ^> ^ ReadFields();
public string[]? ReadFields ();
public string[] ReadFields ();
member this.ReadFields : unit -> string[]
Public Function ReadFields () As String()
返回
- String[]
包含当前行的字段值的字符串数组。An array of strings that contains field values for the current line.
例外
不能使用指定格式分析字段。A field cannot be parsed by using the specified format.
示例
此示例使用 ReadFields 方法从逗号分隔的文件中读取 ParserText.txt 。This example uses the ReadFields method to read from the comma-delimited file ParserText.txt. 此示例将字段写入 Testfile.txt。The example writes the fields to Testfile.txt.
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\ParserText.txt")
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {","}
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
For Each currentField As String In currentRow
My.Computer.FileSystem.WriteAllText(
"C://testfile.txt", currentField, True)
Next
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
End While
End Using
注解
为了使用户能够分析多种格式的文本文件,该 ReadFields 方法将检查、和的值( TextFieldType Delimiters FieldWidths 如果指定了这些值)。In order to allow users to parse text files in multiple formats, the ReadFields method examines the values of TextFieldType, Delimiters, and FieldWidths, if they are specified, each time it is called. 用户需要适当地配置 TextFieldType 和 FieldWidths 或 Delimiters 属性。Users need to correctly configure the TextFieldType and FieldWidths or Delimiters properties, as appropriate. 如果 TextFieldType 设置为 Delimited ,并且 Delimiters 未设置,或者如果 TextFieldType 设置为 FixedWidth 和,则 FieldWidths 引发异常。If TextFieldType is set to Delimited, and Delimiters is not set, or if TextFieldType is set to FixedWidth and FieldWidths, an exception is thrown.
如果 ReadFields 遇到空白行,将跳过这些行,并返回下一个非空行。If ReadFields encounters blank lines, they are skipped and the next non-blank line is returned.
如果该 ReadFields 方法无法分析当前行,则会引发异常,并且不会移动到下一行。If the ReadFields method cannot parse the current line, it raises an exception and does not move to the next line. 这使应用程序能够再次尝试分析该行。This enables your application to attempt to parse the line again.
下表列出了涉及方法的任务的示例 ReadFields 。The following table lists examples of tasks involving the ReadFields method.
| 功能To | 查看See |
|---|---|
| 从带分隔符的文件中读取Read from a delimited file | 如何:读取逗号分隔的文本文件How to: Read From Comma-Delimited Text Files |
| 从固定宽度的文件读取Read from a fixed-width file | 如何:读取固定宽度的文本文件How to: Read From Fixed-width Text Files |
适用于
另请参阅
- OpenTextFieldParser(String)
- TextFieldType
- FieldWidths
- Delimiters
- ReadLine()
- 对象 (Visual Basic)Objects (Visual Basic)
- 如何:在 Visual Basic 中读取 Comma-Delimited 文本文件How to: Read From a Comma-Delimited Text File in Visual Basic
- 如何:在 Visual Basic 中读取固定宽度的文本文件How to: Read From a Fixed-width Text File in Visual Basic
- 如何:在 Visual Basic 中使用多种格式读取文本文件How to: Read From a Text File with Multiple Formats in Visual Basic
- 使用 TextFieldParser 对象分析文本文件Parsing Text Files with the TextFieldParser Object