Sdílet prostřednictvím


Postupy: Jak číst z textových souborů o více formátech v jazyce Visual Basic

TextFieldParser Objekt umožňuje snadno a efektivně rozložit strukturované textové soubory, například protokoly. Můžete zpracovat soubor s více formáty pomocí metody PeekChars a tím při procházení souboru určit formát každého řádku.

Jak analyzovat textový soubor s více formáty

  1. Přidejte textový soubor s názvem testfile.txt do projektu. Přidejte následující obsah do textového souboru.

    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. Definujte očekávaný formát a používaný formát při hlášení chyb. Poslední položky v každé pole -1, tedy v posledním poli považován proměnné šířky. Vyvolá se při poslední položky pole je menší nebo rovna 0.

    Dim stdFormat As Integer() = {5, 10, 11, -1}
    Dim errorFormat As Integer() = {5, 5, -1}
    
  3. Vytvořte nový objekt TextFieldParser, definováním šířky a formátu.

    Using MyReader As New FileIO.TextFieldParser("..\..\testfile.txt")
        MyReader.TextFieldType = FileIO.FieldType.FixedWidth
        MyReader.FieldWidths = stdFormat
    
  4. Iterujte přes řádky a testujte formát před čtením.

    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. Zapisujte chyby do konzoly.

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

Příklad

Následuje příklad úplné, který načte ze souboru 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()

Robustní programování

Následující případy mohou způsobit výjimku:

Viz také

Úkoly

Postupy: Jak číst z textového souboru s čárkou jako oddělovačem v jazyce Visual Basic

Postupy: Jak číst z textových souborů s pevnou šířkou v jazyce Visual Basic

Odkaz

Microsoft.VisualBasic.FileIO.TextFieldParser

PeekChars

MalformedLineException

WriteAllText

EndOfData

TextFieldType

Koncepty

Analýza textových souborů s objektem TextFieldParser (Visual Basic)

Historie změn

Datum

Poslední dokumenty

Důvod

Leden 2011

Byly přidány informace o polích s proměnnou šířkou.

Názory zákazníků