共用方式為


How to: Read From Text Files with Multiple Formats in Visual Basic

The TextFieldParser object provides a way to easily and efficiently parse structured text files, such as logs. You can process a file with multiple formats by using the PeekChars method to determine the format of each line as you parse through the file.

To parse a text file with multiple formats

  1. Add a text file named testfile.txt to your project. Add the following content to the text file.

    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.
    
  2. Define the expected format and the format used when an error is reported. The last entry in each array is -1, therefore the last field is assumed to be of variable width. This occurs when the last entry in the array is less than or equal to 0.

    Dim stdFormat As Integer() = {5, 10, 11, -1}
    Dim errorFormat As Integer() = {5, 5, -1}
    
  3. Create a new TextFieldParser object, defining the width and format.

    Using MyReader As New FileIO.TextFieldParser("..\..\testfile.txt")
        MyReader.TextFieldType = FileIO.FieldType.FixedWidth
        MyReader.FieldWidths = stdFormat
    
  4. Loop through the rows, testing for format before reading.

    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()
    
  5. Write errors to the console.

    Catch ex As Microsoft.VisualBasic.
                        FileIO.MalformedLineException
             MsgBox("Line " & ex.Message & " is invalid.")
          End Try 
       End While 
    End Using
    

Example

Following is the complete example that reads from the file 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()

Robust Programming

The following conditions may cause an exception:

See Also

Tasks

How to: Read From Comma-Delimited Text Files in Visual Basic

How to: Read From Fixed-width Text Files in Visual Basic

Reference

Microsoft.VisualBasic.FileIO.TextFieldParser

PeekChars

MalformedLineException

WriteAllText

EndOfData

TextFieldType

Concepts

Parsing Text Files with the TextFieldParser Object (Visual Basic)