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 ❘ 795<br />

The simplest constructor takes just a filename. This StreamReader examines the byte order marks to<br />

determine the encoding:<br />

StreamReader sr = new StreamReader(@"C:\My Documents\ReadMe.txt");<br />

Alternatively, you can specify that UTF8 encoding should be assumed:<br />

StreamReader sr = new StreamReader(@"C:\My Documents\ReadMe.txt",<br />

Encoding.UTF8);<br />

You specify the encoding by using one of several properties on a class, System.Text.Encoding. This class<br />

is an abstract base class, from which a number of classes are derived <strong>and</strong> which implements methods that<br />

actually perform the text encoding. Each property returns an instance of the appropriate class, <strong>and</strong> the<br />

possible properties you can use are here:<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

ASCII<br />

Unicode<br />

UTF7<br />

UTF8<br />

UTF32<br />

BigEndianUnicode<br />

The following example demonstrates how to hook up a StreamReader to a FileStream. The advantage of<br />

this is that you can specify whether to create the file <strong>and</strong> the share permissions, which you cannot do if you<br />

directly attach a StreamReader to the file:<br />

FileStream fs = new FileStream(@"C:\My Documents\ReadMe.txt",<br />

FileMode.Open, FileAccess.Read, FileShare.None);<br />

StreamReader sr = new StreamReader(fs);<br />

For this example, you specify that the StreamReader will look for byte code markers to determine the<br />

encoding method used, as it will do in the following examples, in which the StreamReader is obtained from<br />

a FileInfo instance:<br />

FileInfo myFile = new FileInfo(@"C:\My Documents\ReadMe.txt");<br />

StreamReader sr = myFile.OpenText();<br />

Just as with a FileStream, you should always close a StreamReader after use. Failure to do so will result in<br />

the file remaining locked to other processes (unless you used a FileStream to construct the StreamReader<br />

<strong>and</strong> specified FileShare.ShareReadWrite):<br />

sr.Close();<br />

Now that you have gone to the trouble of instantiating a StreamReader, you can do something with it. As<br />

with the FileStream, you will simply see the various ways to read data, <strong>and</strong> the other, less commonly used<br />

StreamReader methods are left to the SDK documentation.<br />

Possibly the easiest method to use is ReadLine(), which keeps reading until it gets to the end of a line.<br />

It does not include the carriage return–line feed combination that marks the end of the line in the<br />

returned string:<br />

string nextLine = sr.ReadLine();<br />

Alternatively, you can grab the entire remainder of the file (or strictly, the remainder of the stream) in<br />

one string:<br />

string restOfStream = sr.ReadToEnd();<br />

You can read a single character:<br />

int nextChar = sr.Read();<br />

This overload of Read() casts the returned character to an int. This is so that it has the option of returning<br />

a value of -1 if the end of the stream has been reached.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!