TextFieldParser.ReadFields 메서드

정의

현재 줄의 모든 필드를 읽고, 문자열 배열로 반환하고, 커서를 데이터가 포함된 다음 줄로 진행합니다.

public:
 cli::array <System::String ^> ^ ReadFields();
public string[] ReadFields();
member this.ReadFields : unit -> string[]
Public Function ReadFields () As String()

반품

String[]

현재 줄의 필드 값이 들어 있는 문자열의 배열입니다.

예외

지정된 형식을 사용하여 필드를 구문 분석할 수 없습니다.

예제

이 예제에서는 메서드를 ReadFields 사용하여 쉼표로 구분된 파일 ParserText.txt에서 읽습니다. 이 예제에서는 필드를 .에 Testfile.txt씁니다.

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\ParserText.txt")
    MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    MyReader.Delimiters = New String() {","}
    Dim currentRow As String()
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadFields()
            For Each currentField As String In currentRow
                My.Computer.FileSystem.WriteAllText(
                    "C://testfile.txt", currentField, True)
            Next
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & " is invalid.  Skipping")
        End Try
    End While
End Using

설명

사용자가 여러 형식 ReadFields 으로 텍스트 파일을 구문 분석할 수 있도록 하기 위해 메서드는 호출될 때마다 지정된 경우 , DelimitersFieldWidths값을 검사TextFieldType합니다. 사용자는 적절하게 및 FieldWidths 또는 Delimiters 속성을 올바르게 구성 TextFieldType 해야 합니다. 로 설정Delimited되고 설정되지 않았거나 설정 FixedWidthFieldWidths 되지 않은 경우 TextFieldTypeTextFieldType 예외가 throw Delimiters 됩니다.

빈 줄이 발견되면 ReadFields 해당 줄은 건너뛰고 비어있지 않은 다음 줄이 반환됩니다.

메서드가 ReadFields 현재 줄을 구문 분석할 수 없는 경우 예외가 발생하며 다음 줄로 이동하지 않습니다. 이렇게 하면 애플리케이션에서 줄을 다시 구문 분석할 수 있습니다.

다음 표에서는 메서드와 관련된 ReadFields 작업의 예를 나열합니다.

받는 사람 보십시오
구분된 파일에서 읽기 방법: Comma-Delimited 텍스트 파일에서 읽기
고정 너비 파일에서 읽기 방법: 고정 너비 텍스트 파일에서 읽기

적용 대상

추가 정보