Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))

OpenTextFieldParser Method

Location

My.Computer.FileSystem.OpenTextFieldParser

Syntax

Dim result As FileIO.TextFieldParser = _ My.Computer.FileSystem.OpenTextFieldParser(path[, delimiters])

or:

Dim result As FileIO.TextFieldParser = _ My.Computer.FileSystem.OpenTextFieldParser(path[, fieldWidths])

path (required; String)

The path to the file to be read.

delimiters (optional; String array)

An array of the character or multicharacter values that identify the delimiters used to separate data fields in each input line of a delimited text file being parsed by a TextFieldParser object. End-of-line characters may not be used as field delimiters. If you only require a single delimiter, passing an ordinary string is acceptable.

fieldWidths (optional; Integer array)

An array of values, each of which indicates the character length of a positional field within a fixed-width text file being parsed by a TextFieldParser object. All field widths must be greater than zero, although the last array element may be less than or equal to zero to indicate a final variable-width field.

Description

The OpenTextFieldParser method creates a Microsoft.VisualBasic.FileIO.TextFieldParser object, which represents each data row of the text input file, broken up into distinct fields. You can specify either fixed-position fields or fields delimited by a specific delimiter.

Public Members

See the TextFieldParser Object entry in this chapter for additional information.

Usage at a Glance

  • The delimiters and fieldWidths parameters can also be set with the TextFieldParser's Delimiters and FieldWidths properties or with the SetDelimiters and SetFieldWidths methods.

  • An exception is thrown if the path parameter is missing or invalid, or if the file it refers to does not exist.

  • An exception is thrown if the user lacks sufficient file access permissions.

  • An exception is thrown if a row of the file's text data cannot be parsed according to the parameters supplied through this method. The exception thrown is Microsoft.VisualBasic.FileIO.TextFieldParser.MalformedLineException.

Example

This code opens a tab-delimited file for scanning.

Dim oneLine( ) As String Dim inputFile As FileIO.TextFieldParser = _ My.Computer.FileSystem.OpenTextFieldParser( _ "c:\temp\data.txt", vbTab) Do While Not inputFile.EndOfData oneLine = inputFile.ReadFields( ) ' ----- Process data here... Loop inputFile.Close( )

Related Framework Entries

  • Microsoft.VisualBasic.FileIO.FileSystem.OpenTextFieldParser Method

  • Microsoft.VisualBasic.FileIO.TextFieldParser Class

  • Microsoft.VisualBasic.MyServices.FileSystemProxy.OpenTextFieldParser Method

See Also

FileSystem Object, OpenTextFileReader Method, OpenTextFileWriter Method, ReadAllBytes Method, ReadAllText Method, TextFieldParser Object, WriteAllBytes Method, WriteAllText Method

Категории