Advertisement



< Prev
Next >



C# StreamWriter



C# provides us a character-based stream class i.e. StreamWriter, which allows us to write characters to a byte stream, by wrapping a byte output stream class such as FileStream, MemoryStream, etc. By doing so, the StreamWriter class converts a byte output stream to a character stream.




Some commonly used constructors of StreamWriter


Constructors Description
StreamWriter(System.IO.Stream stream)
This constructor creates an object of the StreamWriter based on the specified stream and using the default UTF-8 encoding.

FileStream(System.IO.Stream stream, System.Text.Encoding encoding)
The constructor created an object of the StreamWriter class based on the specified Stream and character encoding.






Some commonly used write methods of StreamWriter


Methods Description
void Write(char ch)
This method writes a character value to the stream.

void Write(char[] array)
This method writes a character array to the stream. If the array is null, then nothing is written to the output stream.

void Write(char[] array, int index, int total)
This method writes a character array to the stream, where:
  • index, represents the starting index in the char array to write to the output stream.
  • total, represents the total number of characters in char array to write to the output stream.


void Write(String str)
This method writes a String to the stream.

void Flush()
This method clears the buffer for the current writer stream by writing any buffered data to the stream.

void Close()
This method closes this stream and also frees any resources connected with this stream.






In this example, we are going create an object of the StreamWriter class and use it to write a character, character array, part of a character array and a String to a file, by wrapping the byte output stream class FileStream within the StreamWriter class.

This example uses performs the write operation by using the default UTF-8 encoding of StreamWriter class.

//C# Example of StreamWriter by using the default UTF-8 encoding.


using System;
using System.IO;

class A
{
public static void Main(String[] ar)
{
	try
	{
		//A character value
		char ch = 'C';

		//A character array
		char[] arr = {'B', 'l', 'u', 'e', ' ', 'R' ,'a', 'i', 'n'};
			
		//Creating a String object
		String str = "Keep smiling and think positive.";
	

		//Creating an object of FileStream to write to a file
		//We are going to use the FileMode Create to create a new file to write to it
		FileStream fs = new FileStream("D:\\MyFile1.txt", FileMode.Create);


		//Creating an object of StreamWriter to wrap a byte outputStream i.e. FileStream
		//to write to characters to the byte output stream.
		StreamWriter sw = new StreamWriter(fs);

		Console.WriteLine("Writing to the file using StreamWriter and default UTF-8 Encoding");

		//writing a char
		sw.Write(ch);

		//writing a whole char array
		sw.Write(arr);

		//writing a part of char array
		//Starting at its zeroth-index and a total of 4 characters
		sw.Write(arr, 0 , 4);

		//writing a String 
		sw.Write(str);
		
		
		Console.WriteLine("Data written successfully");

		//Calling the Flush() method to flush the buffered data to the file
		sw.Flush();


		//Closing the stream i.e. StreamWriter stream
		//sw.Close();
	}
	catch(IOException e)
	{
		Console.WriteLine(e);
	}
}
}


Output is -:


Writing to the file using StreamWriter and default UTF-8 Encoding
Data written successfully
In this example, we are going create an object of the StreamWriter class and use it to write a character, character array, part of a character array and a String to a file, by wrapping the byte output stream class FileStream within the StreamWriter class.

This example uses writes the characters by by using the non-default Unicode encoding within StreamWriter class.

//C# Example of StreamWriter by using the non-default Unicode encoding.


//C# Example of StreamWriter


using System;
using System.IO;
using System.Text;

class A
{
public static void Main(String[] ar)
{
	try
	{
		//A character value
		char ch = 'C';

		//A character array
		char[] arr = {'B', 'l', 'u', 'e', ' ', 'R' ,'a', 'i', 'n'};
			
		//Creating a String object
		String str = "Keep smiling and think positive.";	
	
		//Creating an object of Encoding type
		Encoding unicode = Encoding.Unicode;


		//Creating an object of StreamWriter to wrap a byte outputStream i.e. FileStream
		//to write to characters to the byte output stream.
		FileStream fs = new FileStream("D:\\MyFile2.txt", FileMode.Create);


		//Creating an object of StreamWriter to write to a file using FileStream 
		StreamWriter sw = new StreamWriter(fs, unicode);

		Console.WriteLine("Writing to the file using StreamWriter and non-default Unicode Encoding");

		//writing a char
		sw.Write(ch);

		//writing a whole char array
		sw.Write(arr);

		//writing a part of char array
		//Starting at its zeroth-index and a total of 4 characters
		sw.Write(arr, 0 , 4);

		//writing a String 
		sw.Write(str);
		
		
		Console.WriteLine("Data written successfully");

		//Calling the Flush() method to flush the buffered data to the file
		sw.Flush();


		//Closing the stream i.e. StreamWriter stream
		//sw.Close();
	}
	catch(IOException e)
	{
		Console.WriteLine(e);
	}
}
}


Output is -:


Writing to the file using BinaryStream and non-default Unicode Encoding
Data written successfully

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement