C# StreamWriter - c# - c# tutorial - c# net




What is StreamWriter Class in C# ?

  • StreamWriter Class is more popular in File Handling and it is very helpful in writing text data in file.
  • It is easy to use and provides complete set of constructors and methods to work.
  • learn c# tutorials - stream writer and stream reader - c# programs

    learn c# tutorial -csharp features - stream writer and stream reader - c# programs - c# Example

  • 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.
 C# StreamWriter

C# StreamWriter

C# StreamWriter example:

  • Let's see a simple example of StreamWriter class which writes a single line of data into the file.
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("wikitechy c#");  

        s.Close();  

        f.Close();  

     Console.WriteLine("File created successfully...");  

    }  
}

C# examples - Output :

File created successfully...
learn c# tutorials - stream writer and stream reader - c# programs

learn c# tutorial -csharp io - stream writer and stream reader - c# programs - c# Example

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

output.txt:

wikitechy c#
Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

SAVE VARIABLE DATA TO FILE:

  • Several times you need to save variable data in a file.
  • These variable data might be output of your program, log details, exception error etc.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace StreamWriter_VariableData
{
class Program
{
static void Main(string[] args)
{
string file = @"D:\csharpfile.txt";
int a, b, result;
a = 5;
b = 10;
result = a + b;

using (StreamWriter writer = new StreamWriter(file))
{
writer.Write("Sum of {0} + {1} = {2}", a, b, result);
}
Console.WriteLine("Saved");
Console.ReadKey();
}
}
}
  • Now open the D:\csharpfile.txt again and you will see the following text in a file.
  • Sum of 5 + 10 = 15


Related Searches to C# StreamWriter

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×