17.2 Files and Streams

C# views each file as a sequential stream of bytes (Fig. 17.1). Each file ends either with an end-of-file marker or at a specific byte number that’s recorded in a system-maintained administrative data structure, because file organization is operating-system dependent. A C# program processing a stream of bytes simply receives an indication from the operating system when it reaches the end of the stream—the program does not need to know how the underlying platform represents files or streams.

Fig. 17.1 C#’s view of an n-byte file.

Standard Streams in Console Apps

When a file is opened, an object is created and a stream is associated with the object. When a console app executes, the runtime environment creates three stream objects that are accessible via properties Console.Out, Console.In and Console.Error, respectively. These objects use streams to 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. These can be redirected to other files or devices. We’ve been using Console.Out and Console.In in our console apps:

  • Console methods Write and WriteLine use Console.Out to perform output, and

  • Console methods Read and ReadLine use Console.In to perform input.

File-Processing Classes

There are many file-processing classes in the Framework Class Library. The System.IO namespace includes stream classes such as StreamReader (for text input from a stream), StreamWriter (for text output to a stream) and FileStream (for both input from and output to a stream). These stream classes inherit from abstract classes TextReader, Text-Writer and Stream, respectively. Console.In and Console.Error are of type TextWriter. Console.In is of type TextReader. The system creates objects of TextReader and Text-Writer derived classes to initialize Console properties Console.In and Console.Out.

Abstract class Stream provides functionality for representing streams as bytes. Classes FileStream, MemoryStream and BufferedStream (all from 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 memory—this 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’s large enough to hold the data from many output operations. Then actual transfer to the output device is performed in one large physical output operation each time the buffer fills. The output operations directed to the output buffer in memory often are called logical output operations. Buffering also can be used to speed input operations by initially reading more data than is required into a buffer, so subsequent reads get data from high-speed memory rather than a slower external device.

In this chapter, we use key stream classes to implement file-processing programs that create and manipulate sequential-access files.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset