方法 : Visual Basic で複数の書式を持つテキスト ファイルを読み取る

更新 : 2007 年 11 月

TextFieldParser オブジェクトを使用すると、ログなどの構造化されたテキスト ファイルを簡単かつ効率的に解析できます。PeekChars メソッドを使用して、ファイルを解析するときに各行の書式を判断することにより、複数の書式を持つファイルを処理できます。

複数の書式を持つテキスト ファイルを解析するには

  1. 目的の書式と、エラーが報告されるときに使用する書式を定義します

    Dim StdFormat As Integer()= {5,10,11,-1}
    Dim ErrorFormat As Integer() = {5,5,-1}
    
  2. 幅と書式を指定して、新しい TextFieldParser オブジェクトを作成します。

    Using MyReader As New _
    Microsoft.VisualBasic.FileIO.TextFieldParser("C:\testfile.txt")
    MyReader.TextFieldType = FileIO.FieldType.FixedWidth
    
  3. 各行をループし、読み取り前に書式を調べます。

    Dim CurrentRow As String()
    While Not MyReader.EndOfData
       Try
          Dim RowType As String = 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)
             CurrentRow = MyReader.ReadFields
             MyReader.SetFieldWidths(StdFormat)
                        Else
             'Otherwise parse the fields normally
             CurrentRow = MyReader.ReadFields
             For Each newString As String In CurrentRow
                My.Computer.FileSystem.WriteAllText _
                ("newFile.txt", newString, True)
              Next
       End If
    
  4. エラーをコンソールに書き込みます。

          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 _
Microsoft.VisualBasic.FileIO.TextFieldParser("C:\testfile.txt")
   MyReader.TextFieldType = FileIO.FieldType.FixedWidth
   MyReader.FieldWidths = StdFormat
   Dim CurrentRow As String()
      While Not MyReader.EndOfData
         Try
            Dim RowType As String = 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)
               CurrentRow = MyReader.ReadFields
               MyReader.SetFieldWidths(StdFormat)
            Else
               ' Otherwise parse the fields normally
               CurrentRow = MyReader.ReadFields
               For Each newString As String In CurrentRow
                  My.Computer.FileSystem.WriteAllText("newFile.txt", newString, True)
               Next
            End If
         Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & " is invalid.  Skipping")
         End Try
      End While
End Using

堅牢性の高いプログラム

次の条件を満たす場合は、例外が発生する可能性があります。

参照

処理手順

方法 : Visual Basic でコンマ区切りのテキスト ファイルを読み取る

方法 : Visual Basic で固定幅のテキスト ファイルを読み取る

概念

TextFieldParser オブジェクトによるテキスト ファイルの解析

参照

TextFieldParser オブジェクト

TextFieldParser.PeekChars メソッド

MalformedLineException

My.Computer.FileSystem.WriteAllText メソッド

TextFieldParser.EndOfData プロパティ

TextFieldParser.TextFieldType プロパティ