TextReader and TextWriter


The TextReader and TextWriter classes are also not stream classes. They provide properties and methods for working with text, which is stream-related. In particular, the StreamWriter and StreamReader classes derived from TextReader and TextWriter are associated with streams.

TextReader and TextWriter are abstract (MustInherit) classes that define behaviors for derived classes that read or write text characters. For example, the StringWriter and StreamWriter classes derived from TextWriter let a program write characters into a string or stream, respectively. Normally, you would use these derived classes to read and write text, but you might want to use the TextReader or TextWriter classes to manipulate the underlying classes more generically. You may also encounter a method that requires a TextReader or TextWriter object as a parameter. In that case, you could pass the method either a StringReader/StringWriter or a StreamReader/StreamWriter. For more information on these, see the sections “StringReader and StringWriter” and “StreamReader and StreamWriter” later in this chapter.

The following table describes the TextReader object’s most useful methods.

Open table as spreadsheet

Method

Purpose

Close

Closes the reader and releases any resources that it is using.

Peek

Reads the next character from the text without changing the reader’s state, so other methods can read the character later.

Read

Reads data from the input. Overloaded versions of this method read a single character or an array of characters up to a specified length.

ReadBlock

Reads data from the input into an array of characters.

ReadLine

Reads a line of characters from the input and returns the data in a string.

ReadToEnd

Reads any remaining characters in the input and returns them in a string.

The TextWriter class has three useful properties. Encoding specifies the text’s encoding (ASCII, UTF-8, Unicode, and so forth).

FormatProvider returns an object that controls formatting. For example, you can build a FormatProvider object that knows how to display numbers in different bases (such as hexadecimal or octal).

The NewLine property gets or sets the string used by the writer to end lines. Usually, this value is something similar to a carriage return or a carriage return plus a line feed.

The following table describes the TextWriter object’s most useful methods.

Open table as spreadsheet

Method

Purpose

Close

Closes the writer and releases any resources it uses.

Flush

Writes any buffered data into the underlying output.

Write

Writes a value into the output. This method has many overloaded versions that write characters, arrays of characters, integers, strings, unsigned 64-bit integers, and so forth.

WriteLine

Writes data into the output followed by the new line sequence.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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