Bagikan melalui


Cara: Membaca dari file teks dengan beberapa format di Visual Basic

Objek TextFieldParser menyediakan cara untuk mengurai file teks terstruktur dengan mudah dan efisien, seperti log. Anda dapat memproses file dengan beberapa format dengan menggunakan PeekChars metode untuk menentukan format setiap baris saat Anda mengurai file.

Untuk mengurai file teks dengan beberapa format

  1. Tambahkan file teks bernama testfile.txt ke proyek Anda. Tambahkan konten berikut ke file teks:

    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. Tentukan format yang diharapkan dan format yang digunakan saat kesalahan dilaporkan. Entri terakhir di setiap array adalah -1, oleh karena itu bidang terakhir diasumsikan memiliki lebar variabel. Ini terjadi ketika entri terakhir dalam array kurang dari atau sama dengan 0.

    Dim stdFormat As Integer() = {5, 10, 11, -1}
    Dim errorFormat As Integer() = {5, 5, -1}
    
  3. Buat objek baru TextFieldParser , menentukan lebar dan format.

    Using MyReader As New FileIO.TextFieldParser("..\..\testfile.txt")
        MyReader.TextFieldType = FileIO.FieldType.FixedWidth
        MyReader.FieldWidths = stdFormat
    
  4. Menelusuri baris-baris, memeriksa format sebelum membaca.

    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. Tulis kesalahan ke konsol.

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

Contoh

Berikut ini adalah contoh lengkap yang membaca dari 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()

Pemrograman yang kuat

Kondisi berikut dapat menyebabkan pengecualian:

Lihat juga