Streams ...
Streams ...
com/lpt/a/3396
On of the common challenges facing beginning .NET programmers is the large number of classes in the .NET
Class Library. Recently, I was working on a project involving file access. I was looking for the correct class to
use for file handling and I realized that there are many classes in the System.IO namespace, each looking not
much different from the others. And so, I set out to document some of the commonly used classes for
performing regular file operations. The examples in the article are by no means exhaustive, but hopefully can
help you in getting started with the right class!
Stream Class
A stream is an abstraction of a sequence of bytes. This sequence of bytes may come from a file, a TCP/IP
socket, or memory. In .NET, a stream is represented, aptly, by the Stream class. The Stream class provides a
generic view of a sequence of bytes. A Stream class forms the abstract class of all other streams. That is, you
cannot directly create an instance of the Stream class. Instead, the Stream class is implemented by the
following classes:
1. Reading
2. Writing
3. Seeking
In this article, I will provide some examples to show the most commonly
used classes for performing file I/O. In particular, I will use FileStream
and MemoryStream, together with the File class for file creation, reading,
and writing. I won’t be discussing each class in detail, as all of these
details can be found from the excellent MSDN documentation that comes VB.NET Core Classes in a
with Visual Studio .NET. Instead, I will just provide simple examples to Nutshell
illustrate the task that we are performing; the subheads of this article will
be grouped by task.
1 of 6 10/8/2010 8:33 PM
ONDotNet.com: .NET Streams Explained http://ondotnet.com/lpt/a/3396
All of the examples in this class make use of the following constants and
variables: By Budi Kurniawan,
Ted Neward
Dim fs, s1, s2 As FileStream
Dim sr As StreamReader
Dim sw As StreamWriter
Dim br As BinaryReader
Dim bw As BinaryWriter
Dim ms As MemoryStream
Besides using FileStream and MemoryStream, I will also make use of the BinaryReader and BinaryWriter
classes for reading and writing binary data, as well as the StreamReader and StreamWriter classes for
reading and writing text data.
And since all of the classes discussed in this article fall under the System.IO namespace, I need to import it:
Imports System.IO
File Management
The File class allows you to create, delete, copy, and rename files. It also allows you to check if a file exists.
The following example checks to see if a file exists, and if it does, makes a copy of the file and then deletes
the original.
File Creation
You can use the File class to create new files as well as open files for reading. The following example first
creates a file and returns a FileStream object. It then creates a new text file and returns a StreamWriter
object. Lastly, it illustrates that you can open a file for reading or writing using the Open() method from the
File class.
fs = File.Create(FILE_NAME_BIN)
fs.Close()
sw = File.CreateText(FILE_NAME)
sw.Close()
2 of 6 10/8/2010 8:33 PM
ONDotNet.com: .NET Streams Explained http://ondotnet.com/lpt/a/3396
To write binary data to a file, you can use the FileStream class. The following example uses the FileStream
class to create a new file for writing. It then writes 11 bytes to the file and closes it.
fs.Write(bytes, 0, bytes.Length)
fs.Close()
For writing text to a file, it is easier to use the StreamWriter class. The following example shows how to use
the StreamWriter class to write a string of text to a file.
To read binary data from a file, use the FileStream class. The following example opens a file and reads the
binary data. The bytes read are then converted to ASCII characters and printed to the Output window.
Seeking
Stream objects can support seeking. The following example shows a FileStream object advancing 11 bytes
and then starting to read the next six bytes.
'---seeking
3 of 6 10/8/2010 8:33 PM
ONDotNet.com: .NET Streams Explained http://ondotnet.com/lpt/a/3396
fs.Seek(11, SeekOrigin.Current)
fs.Read(bytes, 0, 6)
For i = 0 To 5
Console.Write(Chr(bytes(i)))
Next
fs.Close()
When reading text files, you can use the StreamReader class. The following example uses a StreamReader
object to open a file for reading. The file is read line by line, and each line is printed to the Output window.
Reading and Writing Binary Data Using the BinaryReader and BinaryWriter Classes
Besides using the FileStream class for reading and writing binary data, you can also use the BinaryReader
and BinaryWriter classes. The following example reads the binary data from one file and writes it into
another, essentially making a copy of the file. It first uses the FileStream class to open two files--one for
reading and one for writing. The BinaryReader class is then used to read the binary data from the
FileStream, and the BinaryWriter is used to write the binary data to the file.
br = New BinaryReader(s1)
bw = New BinaryWriter(s2)
Sometimes you need to load binary data from file and save it in memory for other uses. A good example is the
PictureBox control in a Windows form. The following example shows how the BinaryReader class is used
to read binary data from a file and then write it to a MemoryStream object. The PictureBox control then uses
the data contained in the MemoryStream as a bitmap image.
4 of 6 10/8/2010 8:33 PM
ONDotNet.com: .NET Streams Explained http://ondotnet.com/lpt/a/3396
Sometimes you may want to convert a string into a byte array, or vice versa, especially when you are using
the Cryptographic APIs. I have provided three functions here to help you make the conversions. The three
functions are:
5 of 6 10/8/2010 8:33 PM
ONDotNet.com: .NET Streams Explained http://ondotnet.com/lpt/a/3396
Return to ONDotnet.com
Copyright © 2009 O'Reilly Media, Inc.
6 of 6 10/8/2010 8:33 PM