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 分析两种类型的文件:带分隔符或固定宽度。 某些属性(例如 Delimiters ,仅在处理带分隔符的文件时)有意义 HasFieldsEnclosedInQuotes ,而仅当处理固定宽度文件时,该 FieldWidths 属性才有意义。

下表列出了涉及 Microsoft.VisualBasic.FileIO.TextFieldParser 对象的任务的示例。

收件人 请参阅
从带分隔符的文本文件中读取 如何:从 Comma-Delimited 文本文件中读取
从固定宽度文本文件中读取 如何:从固定宽度的文本文件中读取
从具有多种格式的文本文件中读取 如何:从具有多种格式的文本文件中读取

构造函数

名称 说明
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 类的新实例。

属性

名称 说明
CommentTokens

定义注释标记。 注释标记是一个字符串,当放置在行的开头时,指示该行是注释,应该被分析器忽略。

Delimiters

定义文本文件的分隔符。

EndOfData

如果 True 当前游标位置与文件的末尾之间没有非空白的非注释行,则返回。

ErrorLine

返回导致最新 MalformedLineException 异常的行。

ErrorLineNumber

返回导致最新 MalformedLineException 异常的行数。

FieldWidths

表示要分析的文本文件中每列的宽度。

HasFieldsEnclosedInQuotes

表示在分析带分隔符的文件时是否用引号括住字段。

LineNumber

返回当前行号,如果流中没有更多字符可用,则返回 -1。

TextFieldType

指示要分析的文件是分隔还是固定宽度。

TrimWhiteSpace

指示是否应从字段值中剪裁前导空格和尾随空格。

方法

名称 说明
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)

适用于

另请参阅