Readers and Writers


Stream readers and writers provide a convenient way to read and write text and binary data types to streams. You might have noticed in our discussions so far that stream Read and Write methods perform I/O using byte-type data arrays. Using byte-type arrays to read and write data makes stream classes inconvenient, especially when you re writing text because your application has to spend a great deal of time converting data types to byte arrays. There are two reader classes ” StreamReader and BinaryReader ” and two writer classes ” StreamWriter and BinaryWriter ” that make handling I/O on streams a snap.

StreamReader and StreamWriter

The best way to read and write character-based data to a stream is by using the StreamReader and StreamWriter classes. Both classes are based on the abstract classes TextReader and TextWriter that enable you to read and write a sequence of characters .

These classes also allow you to read and write characters in different character encoding formats, such as ASCII or Universal Character Set (UCS). Many UCS Transformation Formats (UTF) are available, such as an 8-bit encoding form called UTF-8. Table 2-4 describes the available encoding techniques supported by the StreamReader and StreamWriter classes. By default, these reader/ writer classes use UTF-8 encoding and decoding. However, you can specify other techniques through one of the constructor methods.

Table 2-4: Encoding/Decoding Techniques for Stream Readers/Writers

Technique

Description

ASCII

Unicode characters are encoded/decoded as 7-bit ASCII characters

Big-endian Unicode

Unicode characters are encoded/decoded in big-endian byte form

Unicode

Unicode characters are encoded/decoded as UTF-16 characters

UTF-7

Unicode characters are encoded/decoded as UTF-7 characters

UTF-8

Unicode characters are encoded/decoded as UTF-8 characters

The most useful methods in the StreamWriter class are Write and WriteLine , which come from the TextWriter class. The only difference between the two methods is that WriteLine places a carriage return followed by a line feed at the end of the text and Write does not. Both methods have many prototypes and allow you to read and write many numerical data types and strings as text to a stream. For example, the following code fragment shows how to write a simple character string to a file stream:

C#

 StreamWriterMyStreamWriter=newStreamWriter(MyFileStream); MyStreamWriter.WriteLine(Usingstreamwritersisgreat!); 

Visual Basic .NET

 DimMyStreamWriterAsStreamWriter MyStreamWriter=NewStreamWriter(MyFileStream) MyStreamWriter.WriteLine(Usingstreamwritersisgreat!) 

In the StreamReader class, there s a Read method that allows you to read a single character or a specified number of characters from a stream. There s also a ReadLine method that allows you to read a line of text into a string. Finally, there s a ReadToEnd method that will read the entire contents of the stream and return a string. ReadToEnd is great for downloading a small text file over a network stream. In the companion material samples, we have a sample named TextIO that demonstrates how to read and write text to files using stream readers and writers.

BinaryReader and BinaryWriter

The BinaryReader and BinaryWriter stream classes allow you to read and write specific data types to a stream in binary form, which means that the data types are read and written as they re represented in computer memory. For example, you can write an object of type Int32 and the BinaryWriter will write a 4-byte signed integer to a stream and advance the Position property of the stream by four bytes. We have a downloadable sample named BinaryIO that demonstrates how to read and write data types using binary readers and writers.




Network Programming for the Microsoft. NET Framework
Network Programming for the MicrosoftВ® .NET Framework (Pro-Developer)
ISBN: 073561959X
EAN: 2147483647
Year: 2003
Pages: 121

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