Section 18.3. Files and Streams


18.3. Files and Streams

Visual Basic views a file as a sequential stream of bytes (Fig. 18.2). Depending on the operating system, each file ends either with an end-of-file marker or at a specific byte number that is recorded in a system-maintained administrative data structure. For example, the Windows keeps track of the number of bytes in a file. When you open a file from a Visual Basic program, an object is created and a stream is associated with the object. When a program executes, the runtime environment creates three stream objects that are accessible via properties Console.Out, Console.In and Console.Error, respectively. These objects facilitate communication between a program and a particular file or device. Console.In refers to the standard input stream object, which enables a program to input data from the keyboard. Console.Out refers to the standard output stream object, which enables a program to output data to the screen. Console.Error refers to the standard error stream object, which enables a program to output error messages to the screen. We have been using Console.Out and Console.In in our console applicationsConsole methods Write and WriteLine use Console.Out to perform output, and Console methods Read and ReadLine use Console.In to perform input.

Figure 18.2. Visual Basic's view of an n-byte file.


There are many file-processing classes in the FCL. The System.IO namespace includes stream classes such as StreamReader (for text input from a file), StreamWriter (for text output to a file) and FileStream (for both input from and output to a file). These stream classes inherit from the MustInherit classes Textreader, TextWriter and Stream, respectively. Actually, properties Console.In and Console.Out are of type Textreader and TextWriter, respectively. The system creates objects of Textreader and TextWriter classes to initialize Console properties Console.In and Console.Out.

MustInherit class Stream provides functionality for representing streams that are made of bytes. Classes FileStream, MemoryStream and BufferedStream (all in namespace System.IO) inherit from class Stream. Class FileStream can be used to write data to and read data from files. Class MemoryStream enables the transfer of data directly to and from memorythis is much faster than reading from and writing to external devices. Class BufferedStream uses buffering to transfer data to or from a stream. Buffering is an I/O performance-enhancement technique, in which each output operation is directed to a region in memory, called a buffer, that is large enough to hold the data from many output operations. Then actual transfer to the output device is performed more efficiently in one large physical output operation each time the buffer fills. The output operations directed to the output buffer in memory are often called logical output operations. Buffering can also be used to speed input operations by initially reading more data than is required into a buffer, so that subsequent reads get data from memory rather than an external device.

In this chapter, we use key stream classes to implement file processing programs that create and manipulate sequential-access files. In Chapter 23, Networking: Streams-Based Sockets and Datagrams, we use stream classes to implement networking applications.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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