October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

File I/O with Streams - Part 1 - An Excerpt from Inside C#, Second Edition

  • March 7, 2003
  • By Tom Archer
  • Send Email »
  • More Articles »

String Readers and Writers

Objects of the String class are immutable but that you can process mutable objects of the StringBuilder class. The functionality offered by the StringReader and StringWriter classes overlaps somewhat with that of both the MemoryStream and BufferedStream classes and the String and StringBuilder classes. You can use StringWriter to build a mutable string in memory and extract from it either a String or a StringBuilder class. StringWriter has a Write and a WriteLine method that function almost identically to those in the Console class, and you can build the internal string in a StringWriter by using any technique that works with a raw string. For example:
class StringReadWriteApp
{
  static void Main(string[] args)
  {
    StringWriter w = new StringWriter();
    w.WriteLine("Sing a song of {0} pence", 6);
    string s = "A pocket full of rye";
    w.Write(s);
    w.Write(w.NewLine);
    w.Write(String.Format(4 +" and " +20 +" blackbirds"));
    w.Write(new StringBuilder(" baked in a pie"));
    w.WriteLine();
    w.Close();
    Console.WriteLine(w);
  }
}

Here's the output:

Sing a song of 6 pence
A pocket full of rye
4 and 20 blackbirds baked in a pie

Clearly, when we pass the StringWriter reference to Console.WriteLine, the ToString method is being invoked. Note the call to StringWriter.Close in the preceding code. Although there's a Flush method in the StringWriter class, flushing the stream won't flush its underlying encoder unless you explicitly call Close. Closing the stream will automatically flush it and will ready it for destruction by calling Dispose. It.s also instructive to step through this code in the debugger, where you.ll clearly see that the StringWriter contains an internal StringBuilder field named _sb, which in turn contains a String field named m_StringValue. Figure 1 shows these fields.



Figure 1 : StringWriter, StringBuilder, and String. (Click here for larger image)

Let.s continue with this example to modify the string a little more: get a reference to the internal StringBuilder field, and make changes to the StringWriter via that reference, as shown next. Keep the call to the Close method at the bottom, after you.ve finished working with the stream.

StringBuilder sb = w.GetStringBuilder();
int i = sb.Length;
sb.Append("The birds began to sing");
sb.Insert(i, "When the pie was opened\n");
sb.AppendFormat("\nWasn't that a {0} to set before the King", "dainty dish");
Console.WriteLine(w);

The additional output from this block of code is shown here:

Sing a song of 6 pence
A pocket full of rye
4 and 20 blackbirds baked in a pie
When the pie was opened
The birds began to sing
Wasn't that a dainty dish to set before the King

The StringReader class is very simple. We can construct a StringReader from the string in the StringWriter and then use the Read, ReadLine, ReadBlock, and ReadToEnd methods to read characters from the string. This additional code will produce the same output as the previous code block did:

Console.WriteLine();
StringReader r = new StringReader(w.ToString());
string t = r.ReadLine();
Console.WriteLine(t);
Console.Write((char)r.Read());
char[] ca = new char[37];
r.Read(ca, 0, 19);
Console.Write(ca);
r.ReadBlock(ca, 0, 37);
Console.Write(ca);
Console.WriteLine(r.ReadToEnd());
r.Close();
w.Close();

Read will read either one character or a block of characters.in which case, it behaves exactly as ReadBlock does.



Page 4 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel