如何:从具有多种格式的文本文件中读取 - Visual Basic

如何:从具有多种格式的文本文件中读取 - Visual Basic

该 TextFieldParser 对象提供了一种轻松高效地分析结构化文本文件(如日志)的方法。 可以使用该方法在分析文件时确定每行的格式,从而处理具有多种格式 PeekChars 的文件。

分析具有多种格式的文本文件

将名为 testfile.txt 的文本文件添加到项目中。 将以下内容添加到文本文件:

Err 1001 Cannot access resource.

Err 2014 Resource not found.

Acc 10/03/2009User1 Administrator.

Err 0323 Warning: Invalid access attempt.

Acc 10/03/2009User2 Standard user.

Acc 10/04/2009User2 Standard user.

定义报告错误时使用的预期格式和格式。 每个数组中的最后一个条目为 -1,因此假定最后一个字段为可变宽度。 当数组中的最后一个条目小于或等于 0 时,将发生这种情况。

Dim stdFormat As Integer() = {5, 10, 11, -1}

Dim errorFormat As Integer() = {5, 5, -1}

创建新 TextFieldParser 对象,定义宽度和格式。

Using MyReader As New FileIO.TextFieldParser("..\..\testfile.txt")

MyReader.TextFieldType = FileIO.FieldType.FixedWidth

MyReader.FieldWidths = stdFormat

依次通过各行,并在读取之前测试格式。

Dim currentRow As String()

While Not MyReader.EndOfData

Try

Dim rowType = MyReader.PeekChars(3)

If String.Compare(rowType, "Err") = 0 Then

' If this line describes an error, the format of the row will be different.

MyReader.SetFieldWidths(errorFormat)

Else

' Otherwise parse the fields normally

MyReader.SetFieldWidths(stdFormat)

End If

currentRow = MyReader.ReadFields

For Each newString In currentRow

Console.Write(newString & "|")

Next

Console.WriteLine()

将错误写入控制台。

Catch ex As Microsoft.VisualBasic.

FileIO.MalformedLineException

MsgBox("Line " & ex.Message & " is invalid.")

End Try

End While

End Using

示例:

以下是从文件 testfile.txt 中读取的完整示例:

Dim stdFormat As Integer() = {5, 10, 11, -1}

Dim errorFormat As Integer() = {5, 5, -1}

Using MyReader As New FileIO.TextFieldParser("..\..\testfile.txt")

MyReader.TextFieldType = FileIO.FieldType.FixedWidth

MyReader.FieldWidths = stdFormat

Dim currentRow As String()

While Not MyReader.EndOfData

Try

Dim rowType = MyReader.PeekChars(3)

If String.Compare(rowType, "Err") = 0 Then

' If this line describes an error, the format of the row will be different.

MyReader.SetFieldWidths(errorFormat)

Else

' Otherwise parse the fields normally

MyReader.SetFieldWidths(stdFormat)

End If

currentRow = MyReader.ReadFields

For Each newString In currentRow

Console.Write(newString & "|")

Next

Console.WriteLine()

Catch ex As FileIO.MalformedLineException

MsgBox("Line " & ex.Message & " is invalid. Skipping")

End Try

End While

End Using

Console.ReadLine()

可靠编程

以下条件可能会导致异常:

无法使用指定的格式MalformedLineException解析行。 异常消息指定导致异常的行,而 ErrorLine 该属性将分配给行中包含的文本。

指定的文件不存在(FileNotFoundException)。

用户没有足够的权限访问文件,这是一种部分信任的情况。 (SecurityException)。

路径太长(PathTooLongException)。

用户没有足够的权限来访问文件(UnauthorizedAccessException)。

另请参阅

Microsoft.VisualBasic.FileIO.TextFieldParser

PeekChars

MalformedLineException

WriteAllText

EndOfData

TextFieldType

如何:从 Comma-Delimited 文本文件中读取

如何:从固定宽度的文本文件中读取

使用 TextFieldParser 对象分析文本文件

相关手记

365bet取款要多久 BHC炸鸡加盟

BHC炸鸡加盟

11-06 👁️ 8900
365bet取款要多久 成就列表-魔兽世界正式服成就
365bet取款要多久 天然钻石有磷光吗?钻石磷光和荧光的区别
beat365英超欧冠比分 《俠盜獵車手5》MOD安裝圖文教學
365bet取款要多久 3D看房系统需要多少钱?
365彩票最新版app下载 友宝售货机

友宝售货机

08-20 👁️ 3801