The .NET Framework includes a new object-oriented approach to reading and writing files: streams. The abstract
Stream object, found at
System.IO.Stream, defines a generic interface to a chunk of data. It doesn't matter where that data is: in a file, in a block of memory, in a
String variable—if you have a block of data that can be read or written one byte at a time, you can design a derived stream class to interact with it.
The basic features of a
Stream object include the
Write methods that let you read or write bytes. As data is read from or written to a stream, the
Stream object maintains a "current position" within the stream that you can adjust using the
Seek method, or examine using the
Position property. The
Length property indicates the size of the readable data. The class also exposes variations of these basic features to allow as much flexibility as possible.
Not every stream supports all features. Some streams are read-only, forward-only constructs that don't support writing or seeking. Other streams support all possible features. The features available to you depend on the type of stream you use. Since
Stream itself is abstract, you must create an instance of one of its derived classes. .NET defines several useful streams ready for your use:
FileStream object lets you access the content of a file using the basic methods of the generic
FileStream objects support reading, writing, and seeking, ...