Reading and Writing Data

Reading and writin g data is accomplished with the Stream class. Remember streams? This is a chapter about streams.[11]

Stream supports synchronous and asynchronous reads and writes. The .NET Framework provides a number of classes derived from Stream, including FileStream, MemoryStream, and NetworkStream. In addition, there is a BufferedStream class, which provides buffered I/O and which can be used in conjunction with any of the other stream classes. The principal classes involved with I/O are summarized in Table 21-5.

Table 21-5. Principle I/O classes of the .NET Framework




Abstract class that supports reading and writing bytes.


Read and write encoded strings and primitive datatypes to and from streams.

File , FileInfo , Directory , DirectoryInfo

Provide implementations for the abstract FileSystemInfo classes, including creating, moving, renaming, and deleting files and directories.


For reading to and from File objects; supports random access to files. Opens files synchronously by default; supports asynchronous file access.

TextReader , TextWriter, StringReader , StringWriter

TextReader and TextWriter are abstract classes designed for Unicode character I/O. StringReader and StringWriter write to and from strings, allowing your input and output to be either a stream or a string.


A stream that adds buffering to another stream such as a NetworkStream ...

Get Programming C#, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.