15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

eading <strong>and</strong> Writing to files ❘ 787<br />

Grease<br />

Close Encounters of the Third Kind<br />

The Day After Tomorrow<br />

The WriteAllLines() method writes out the string array with each array item taking its own line<br />

in the file.<br />

Because data may be written not only to disk but to other places as well (such as to named pipes or to<br />

memory), it is also important to underst<strong>and</strong> how to deal with file I/O in .<strong>NET</strong> using streams as a means of<br />

moving file contents around. This is shown in the following section.<br />

streams<br />

The idea of a stream has been around for a very long time. A stream is an object used to transfer data. The<br />

data can be transferred in one of two directions:<br />

➤<br />

➤<br />

If the data is being transferred from some outside source into your program, it is called reading from<br />

the stream.<br />

If the data is being transferred from your program to some outside source, it is called writing to<br />

the stream.<br />

Very often, the outside source will be a file, but that is not always the case. Other possibilities include:<br />

➤<br />

➤<br />

➤<br />

Reading or writing data on the network using some network protocol, where the intention is for this<br />

data to be picked up by or sent from another computer<br />

Reading or writing to a named pipe<br />

Reading or writing to an area of memory<br />

Of these examples, Microsoft has supplied a .<strong>NET</strong> base class for writing to or reading from memory, the<br />

System.IO.MemoryStream object. The System.Net.Sockets.NetworkStream object h<strong>and</strong>les network<br />

data. There are no base stream classes for writing to or reading from pipes, but there is a generic stream<br />

class, System.IO.Stream, from which you would inherit if you wanted to write such a class. Stream does<br />

not make any assumptions about the nature of the external data source.<br />

The outside source might even be a variable within your own code. This might sound paradoxical, but the<br />

technique of using streams to transmit data between variables can be a useful trick for converting data<br />

between data types. The C language used something similar — the sprintf function — to convert between<br />

integer data types <strong>and</strong> strings or to format strings.<br />

The advantage of having a separate object for the transfer of data, rather than using the FileInfo or<br />

DirectoryInfo classes to do this, is that separating the concept of transferring data from the particular<br />

data source makes it easier to swap data sources. Stream objects themselves contain a lot of generic code<br />

that concerns the movement of data between outside sources <strong>and</strong> variables in your code. By keeping this<br />

code separate from any concept of a particular data source, you make it easier for this code to be reused<br />

(through inheritance) in different circumstances. For example, the StringReader <strong>and</strong> StringWriter<br />

classes are part of the same inheritance tree as two classes that you will be using later on to read <strong>and</strong> write<br />

text files. The classes will almost certainly share a substantial amount of code behind the scenes.<br />

Figure 29-8 illustrates the actual hierarchy of stream-related classes in the System.IO namespace.<br />

As far as reading <strong>and</strong> writing files, the classes that concern us most are:<br />

➤<br />

➤<br />

FileStream — This class is intended for reading <strong>and</strong> writing binary data in a binary file. However,<br />

you can also use it to read from or write to any file.<br />

StreamReader <strong>and</strong> StreamWriter — These classes are designed specifically for reading from <strong>and</strong><br />

writing to text files.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!