Chapter 31. 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, including 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.

UNDERSTANDING STREAMS

There are many kinds of files: web pages, video, audio, executable, and many 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 enables you to treat them uniformly. That is, you can define common classes and methods that you can then use to manipulate any kind of file.

Many programming languages, including Visual Basic, 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.

Note

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 some complex process such as encryption or compression. This lesson focuses on file streams.

Stream objects provide methods for manipulating streams at a low level. For example, the Stream class provides Read and Write

Get Stephens' Visual Basic® Programming 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.