CHAPTER 16 THE SYSTEM.IO NAMESPACE 537 static (Hosting web)

CHAPTER 16 THE SYSTEM.IO NAMESPACE 537 static void Main(string[] args) { Console.WriteLine(”***** Fun with StringWriter / StringReader *****n”); // Create a StringWriter and emit character data to memory. StringWriter strWriter = new StringWriter(); strWriter.WriteLine(”Don’t forget Mother’s Day this year…”); strWriter.Close(); // Get a copy of the contents (stored in a string) and pump // to console. Console.WriteLine(”Contents of StringWriter:n{0}”, strWriter); } Because StringWriter and StreamWriter both derive from the same base class (TextWriter), the writing logic is more or less identical. However, given that nature of StringWriter, be aware that this class allows you to extract a System.Text.StringBuilder object via the GetStringBuilder() method: static void Main(string[] args) { Console.WriteLine(”***** Fun with StringWriter / StringReader *****n”); // Create a StringWriter and emit character data to memory. StringWriter strWriter = new StringWriter(); … // Get the internal StringBuilder. StringBuilder sb = strWriter.GetStringBuilder(); sb.Insert(0, “Hey!! “); Console.WriteLine(”-> {0}”, sb.ToString()); sb.Remove(0, “Hey!! “.Length); Console.WriteLine(”-> {0}”, sb.ToString()); } When you wish to read from a stream of character data, make use of the corresponding StringReader type, which (as you would expect) functions identically to the related StreamReader class. In fact, the StringReader class does nothing more than override the inherited members to read from a block of character data, rather than a file, as shown here: static void Main(string[] args) { Console.WriteLine(”***** Fun with StringWriter / StringReader *****n”); // Create a StringWriter and emit character data to memory. StringWriter strWriter = new StringWriter(); … // Read data from the StringWriter. StringReader strReader = new StringReader(writer.ToString()); string input = null; while ((input = strReader.ReadLine()) != null) { Console.WriteLine (input); } strReader.Close(); } Source Code The StringReaderWriterApp is included under the Chapter 16 subdirectory.
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Leave a Reply