Chapter 28: Streams


Overview

At some very primitive level, all pieces of data are just piles of bytes. The computer doesn’t really store invoices, employee records, and recipes. At its most basic level, the computer stores bytes of data (or even bits, but the computer naturally groups them in bytes). It is only when a program interprets those bytes that they acquire a higher-level meaning that is valuable to the user.

Although you generally don’t want to treat high-level data as undifferentiated bytes, there are times when thinking of the data as bytes lets you treat it in more uniform ways.

One type of byte-like data is the stream, an ordered series of bytes. Files, data flowing across a network, messages moving through a queue, and even the memory in an array all fit this description.

Defining the abstract idea of a stream lets applications handle these different types of objects uniformly. If an encryption or serialization routine manipulates a generic stream of bytes, it doesn’t need to know whether the stream represents a file, a chunk of memory, or data flowing across a network.

Visual Studio provides several classes for manipulating different kinds of streams. It also provides higher-level classes for working with this kind of data at a more abstract level. For example, it provides classes for working with streams that happen to represent files and directories.

This chapter describes some of the classes you can use to manipulate streams. It explains lower-level classes that you may use only rarely and higher-level classes that let you read and write strings and files relatively easily.

The following table summarizes the most useful stream classes.

Open table as spreadsheet

Class

Use

FileStream

Read and write bytes in a file.

MemoryStream

Read and write bytes in memory.

BinaryReader, BinaryWriter

Read and write specific data types in a stream.

StringReader, StringWriter

Read and write text with or without new lines in a string.

StreamReader, StreamWriter

Read and write text with or without new lines in a stream (usually a file stream).




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