Create TextWriter via StreamWriter and MemoryStream - CSharp File IO

CSharp examples for File IO:MemoryStream

Description

Create TextWriter via StreamWriter and MemoryStream

Demo Code

using System;//from   ww  w  . j a v  a 2  s. c om
using System.IO;
public class Program
{
   public static void Main(string[] args)
   {
      string data = "test/ntest/ntest/n";  // \n equiv to 2 bytes, so 18 bytes.
      Console.WriteLine("Original data:\n{0}", data);
      byte[] buffer = new byte[data.Length + 20];
      MemoryStream ms = new MemoryStream(buffer);
      TextWriter writer = new StreamWriter(ms);
      if (ms.CanWrite)
      {
         Console.WriteLine("Can write to stream.");
      }
      writer.WriteLine(data);
      writer.Flush();
   }
}

Result


Related Tutorials