C# StreamWriter

C# StreamWriter class is used to write characters to a stream in specific encoding. It inherits TextWriter class. It provides overloaded write() and writeln() methods to write data into file.

Example

Let's see a simple example of StreamWriter class which writes a single line of data into the file.

snippet
using System;
using System.IO;
public class StreamWriterExample
{
    public static void Main(string[] args)
    {
        FileStream f = new FileStream("e:\\output.txt", FileMode.Create);
        StreamWriter s = new StreamWriter(f);

        s.WriteLine("hello c#");
        s.Close();
        f.Close();
	 Console.WriteLine("File created successfully...");
    }
}
Output
File created successfully...

Now open the file, you will see the text "hello c#" in output.txt file.

output.txt

hello c#
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +