File IO Classes: An Overview


File I/O Classes: An Overview

The stream classes are located in the System.IO namespace, together with other classes that can be used to manage the directories and files on your computer. To get an overview of these classes, you can briefly browse through the .NET Framework documentation of the System.IO namespace.

As you gradually get to know the stream classes, you will see that there are usually several ways (perhaps involving different classes) to accomplish the same end result. In this brief presentation, I have chosen a few frequently used ways to get some simple jobs done and will only look at a small subset of the many available classes in the System.IO namespace.

Table 22.1 contains the classes we will look at in the following sections. A FileInfo instance encapsulates a file in a directory on your computer's file system. It contains numerous methods for manipulating a file and for discovering details about it. The FileInfo class is discussed in more detail in the next section.

Table 22.1. Fundamental System.IO Classes
System.IO class Description
FileInfo An instance of FileInfo represents a file in your computer's file system. It lets you modify and gather details about the file it represents, and it enables you to create new files and delete existing files.
Character-based input and output streams.
FileStream Used to read and write to binary files.

Instances of the StreamReader and StreamWriter classes represent character streams by which you can read and write text to a text file.

An instance of the FileStream will read and write both text and binary files. However, if you are certain that you are dealing with a text file, the StreamReader and StreamWriter classes contain facilities that make this task easier to accomplish.

Notice that a FileInfo object does not represent a stream object. Instead, you can use its OpenText and CreateText methods to return StreamReader and StreamWriter objects, providing stream access to the file represented by the FileInfo object.

Similarly, you can use a FileInfo object's OpenRead and OpenWrite methods to return a FileStream object that can be used to read and write to either the associated binary file or text file.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: