Streams for new ASP.NET Web Forms programmers

In your previous studies, you have typically worked with programming language functions that work with files – open, close, and so on. Today, you will begin to learn about a higher-level abstraction called streams.

.

Now that you have some experience with file uploads and file downloads, we will look closely at the data stream that flows between end points.

The stream is the foundation object that enables data movement between end points.

Streams and files are different from each other, but they are closely related because a stream is used to read from or write to a file.

.

Introduction to streams

This is what the MSDN File and Stream I/O introductory page has to say:

A file is an ordered and named collection of a particular sequence of bytes having persistent storage. Therefore, with files, one thinks in terms of directory paths, disk storage, and file and directory names.

Streams provide a way to write and read bytes to and from a backing store that can be [located on] one of several storage mediums.

There are several kinds of streams, [including file,] network, memory, and tape streams.

.

From the MSDN Basic File I/O topic page, discussion about streams continues:

The abstract base class Stream supports reading and writing bytes.

All classes that represent streams inherit from the Stream class. The Stream class and its derived classes provide a generic view of data sources and repositories [i.e. the “backing store” from above], isolating the programmer from the specific details of the operating system and underlying devices.

Streams involve these fundamental operations:

  • Streams can be read from. Reading is the transfer of data from a stream into a data structure, such as an array of bytes.
  • Streams can be written to. Writing is the transfer of data from a data source into a stream.
  • [Some types of] streams can support seeking. Seeking is the querying and modifying of the current position within a stream.

.

Here are a few examples of streams:

SOURCE
end point
Kind of data flowing between end points TARGET
end point
.NET Framework implementation
Web browser client’s file system – file object Binary or byte-oriented (e.g. character or string) data Web server’s file system, in a folder that is accessible to the receiving web application FileUpload web server control
Text (byte-oriented) file Byte-oriented data String instance in a program StreamReader that uses a File object constructor
Binary file (e.g. a PNG or JPG image) Binary data, usually processed in chunks of 8, 16, 32, 64 (etc.) bits Byte array (for data that is processed in 8-bit chunks); and this object may get used for other purposes (graphics drawing object, or sent to a database server so that it can be stored) MemoryStream that uses a byte array constructor

.

How we will use streams in file I/O

For our introduction to file I/O programs this week, we will use the StreamReader class to read data from a text file, and the StreamWriter class to write data to a text file.

.

StreamReader

A StreamReader is designed to get data from a standard text file. The following are a few highlights of using a StreamReader:

  • For programming convenience, make sure you are “using System.IO;” in your programs
  • You must create an instance of a StreamReader
  • Its constructor can directly reference a file (using a static method from the File class; this class is really covered in the next section today)
  • The constructor “opens” the file for processing, so you don’t have to explicitly do that
  • Notable reading methods include Read(), ReadLine(), and ReadToEnd()
  • To determine the end of file condition, use one of the ways in today’s example
  • When you’re finished processing the file, you must Close() the StreamReader

.

StreamWriter

A StreamWriter is designed to write data to a standard text file. The following are a few highlights of using a StreamWriter:

  • For programming convenience, make sure you are “using System.IO;” in your programs
  • You must create an instance of a StreamWriter
  • Its constructor can directly reference a file (using a static method from the File class)
  • The constructor “creates” the file, so you don’t have to explicitly do that
  • Notable writing methods include Write() an WriteLine()
  • When you’re finished processing the file, you must Close() the StreamWriter

.

For best results, wrap your file I/O operations in a try-catch-finally block. Place the Close() method call in the finally block. See today’s examples for best practices and reasons.

If you want to do more than just read a file (i.e. if you want to create, modify/write, delete), you must use impersonation to enable your code to do so.

.

Please note that both StreamReader and StreamWriter offer many different constructors to cover many different usage scenarios. They’re very flexible.

Also, please note that we are introducing file I/O using simple examples that should be considered single-user in operation. If your application requires multi-user processing, you need to use thread-safe implementations of stream objects.

.


  1. No comments yet.
  1. No trackbacks yet.

Leave a comment