Streams

Streams

A stream is essentially an abstraction of a sequence of bytes, such as a file, an input/output device, an interprocess communication pipe, or even a TCP/IP socket. The .NET Stream class is the abstract base class of all .NET stream classes. The Stream class and its derived classes, such as FileStream, provide a generic view of these different types of input and output. The StreamWriter class is designed for character output, and subclasses of Stream are designed for byte input and output. These classes are incredibly powerful because they isolate the programmer from the specific details of the operating system and the underlying devices.

What's the Difference Between a File and a Stream?

The difference between a file and a stream is not always hard and fast. You can think of a file as an ordered and named collection of a particular sequence of bytes having persistent storage. When we think of files, we think of concepts such as directory paths, disk storage, and file and directory names.

Although streams also provide a way to write and read bytes to and from a backing store, the location can be one of several storage mediums. Just as there are backing stores other than disks, there are several kinds of streams other than file streams. For example, there are network and tape streams. The .NET Framework even provides a MemoryStream class that is a nonbuffered stream whose encapsulated data is directly accessible in computer memory. This type of stream has no backing store and may be useful as a temporary buffer.

Streams have some fundamental operations such as reading and writing. Also, streams can support seeking, which is the ability to move an internal pointer that indicates the current position in the stream. As with anything else, working with streams includes some exceptions. Depending on the backing store of the stream, only some stream operations might be supported. For example, the NetworkStream class does not support seeking. You can access the stream properties CanRead, CanWrite, and CanSeek to determine which operations a specific stream supports.

Reading and Writing Binary, Numeric, or Text Data

As you know, files can contain strings, numeric data types, and binary data. Depending on our needs, we will need to determine what type of stream we need in order to work with the data at hand. The File class supports all of these data types and allows you to incorporate data types in files.

The System.IO class contains many inherited classes that allow you to read and write characters to and from streams or files, using specific encoding to convert characters to and from bytes. Depending on what we need for the problem at hand, we'll frequently use classes such as FileStream to read and write files on a file system, BinaryReader and BinaryWriter to read and write binary information, StreamReader and StreamWriter to read and write character information in a specific coding—such as Universal Character Set Transformation Format 8-bit (UTF-8)—and TextReader and TextWriter to read and write a series of characters.

We will examine the StreamReader and StreamWriter classes in the examples that follow, but you'll be able to use any of the reader or writer classes because they are implemented in the same way.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

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