Files and Streams
String Writer
Build text with writer methods before reading the finished string.
streams
A writer collects text through method calls instead of one long expression.
String Writer
StringWriterExample.cs
Replay: real traced execution (multi-file project)
using System;
using System.IO;
class Program
{
static void Main()
{
string item = "tea";
using StringWriter writer = new StringWriter();
writer.Write("order:");
writer.WriteLine(item);
writer.Write("done");
string result = writer.ToString().Replace("\n", "|").Replace("\r", "");
Console.WriteLine($"item={item}");
Console.WriteLine($"result={result}");
}
}
using System;
using System.IO;
class Program
{
static void Main()
{
string item = "coffee";
using StringWriter writer = new StringWriter();
writer.Write("order:");
writer.WriteLine(item);
writer.Write("done");
string result = writer.ToString().Replace("\n", "|").Replace("\r", "");
Console.WriteLine($"item={item}");
Console.WriteLine($"result={result}");
}
}
using System;
using System.IO;
class Program
{
static void Main()
{
string item = "juice";
using StringWriter writer = new StringWriter();
writer.Write("order:");
writer.WriteLine(item);
writer.Write("done");
string result = writer.ToString().Replace("\n", "|").Replace("\r", "");
Console.WriteLine($"item={item}");
Console.WriteLine($"result={result}");
}
}
item ← tea, writer ← (empty), result ← order:tea|done
5{6 static void Main()7 {8 string item→ tea = "tea"; //@item="coffee", "juice"910 using StringWriter writer→ (empty) = new StringWriter();11 writer→ order:.Write("order:");12 writer→ order:tea .WriteLine(itemtea);13 writer→ order:tea done.Write("done");1415 string result→ order:tea|done = writerorder:tea done.ToString().Replace("\n", "|").Replace("\r", "");1617 Console.WriteLine($"item={itemtea}");18 Console.WriteLine($"result={resultorder:tea|done}");19 }outputitem=tea result=order:tea|done
item ← coffee, writer ← (empty), result ← order:coffee|done
5{6 static void Main()7 {8 string item→ coffee = "coffee";910 using StringWriter writer→ (empty) = new StringWriter();11 writer→ order:.Write("order:");12 writer→ order:coffee .WriteLine(itemcoffee);13 writer→ order:coffee done.Write("done");1415 string result→ order:coffee|done = writerorder:coffee done.ToString().Replace("\n", "|").Replace("\r", "");1617 Console.WriteLine($"item={itemcoffee}");18 Console.WriteLine($"result={resultorder:coffee|done}");19 }outputitem=coffee result=order:coffee|done
item ← juice, writer ← (empty), result ← order:juice|done
5{6 static void Main()7 {8 string item→ juice = "juice";910 using StringWriter writer→ (empty) = new StringWriter();11 writer→ order:.Write("order:");12 writer→ order:juice .WriteLine(itemjuice);13 writer→ order:juice done.Write("done");1415 string result→ order:juice|done = writerorder:juice done.ToString().Replace("\n", "|").Replace("\r", "");1617 Console.WriteLine($"item={itemjuice}");18 Console.WriteLine($"result={resultorder:juice|done}");19 }outputitem=juice result=order:juice|done