OpenTextFileWriter Method


OpenTextFileWriter Method

Location

My.Computer.FileSystem.OpenTextFileWriter

Syntax

     Dim result As System.IO.StreamWriter = _        My.Computer.FileSystem.OpenTextFileWriter( _    path, append[, encoding]) 


path (required; String)

The path of the file to written.


append (required; Boolean)

Indicates whether newly written text will be appended to the end of the file's existing content (true) or used to replace any existing content (False).


encoding (optional; Encoding)

The character-encoding method to use, as a System.Text.Encoding object. If this parameter is missing, ASCII is used as the default encoding method.

Description

The OpenTextFileWriter opens or creates a file for text writing and returns a stream of type System.IO.StreamWriter.

Public Members

The returned StreamWriter object includes the following notable public members.

Member

Description

Close

Method. Completes all pending writes and closes the file.

Flush

Method. Forces any pending buffered writes to be sent to the file immediately. You can also set the related AutoFlush property to true to perform a Flush after every write.

Write

Method. Writes data to the stream.

WriteLine

Mathod. Writes data to the stream, followed by a line-termination character.


Usage at a Glance

  • An exception is thrown if the path parameter is missing or invalid or if the file exists and is in use.

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

  • If the file already exists and uses an encoding method other than the one specified by this method, the original encoding method of the file will prevail.

Example

The following code creates or overwrites a file with some simple text data.

     Dim counter As Integer     Dim outputFile As System.IO.StreamWriter = _        My.Computer.FileSystem.OpenTextFileWriter( _        "c:\temp\data.txt", False)  ' False = Overwrite     For counter = 1 To 10        outputFile.WriteLine("This is line " & counter & ".")     Next counter     outputFile.Close(  ) 

Related Framework Entries

  • Microsoft.VisualBasic.FileIO.FileSystem.OpenTextFileWriter Method

  • Microsoft.VisualBasic.MyServices.FileSystemProxy.OpenTextFileWriter Method

  • System.IO.StreamWriter Class

  • System.IO.TextWriter Class

See Also

FileSystem Object, OpenTextFieldParser Method, OpenTextFileReader Method, ReadAllBytes Method, ReadAllText Method, WriteAllBytes Method, WriteAllText Method




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net