
Reading and Writing Files
Files play an extremely important role on a computer. They hold text, pictures, Microsoft Word
documents, spreadsheets, and all sorts of other data. They also hold executable programs includ-
ing those that provide the operating system itself.
In this lesson you learn some basic techniques for reading and writing text files. Using some
fairly simple techniques, you can use text files to store and retrieve data used by a program.
This is one of those topics where there are many ways to perform the same
tasks. There are lots of approaches to manipulating files, and this lesson
describes only one.
UNDERSTANDING STREAMS
There are many kinds of files: web pages, video, audio, executable, and lots of others. At
some level, however, files are all the same. They’re just a series of bytes stored on a file
system somewhere.
Thinking about files at this very low level lets you treat them uniformly. It lets you define
common classes and methods that you can use to manipulate any kind of file.
Many programming languages, including C#, make working with files at a low level easier by
defining the concept of a stream. A stream is simply an ordered series of bytes.
Streams can also represent things other than files. For example, a stream could
represent data being sent from one program to another, a series of bytes being
downloaded from a web site, or the flow of data as it moves through ...