Advertisement



< Prev
Next >



C# StreamReader



In our last tutorial, we have explained how you to use the StreamWriter class, which is used to write characters to a byte stream, by wrapping a byte output stream.

In this tutorial, we are going to explain the counterpart of StreamWriter class i.e. StreamReader class, which is used to create a stream that can be used to read the characters by wrapping a byte input stream. Doing so, converts the byte stream to a character stream and allows us to read characters from it.




Some commonly used constructors of StreamReader


Constructors Description
StreamReader(System.IO.Stream stream)
This constructor creates an object of the StreamReader 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 StreamReader class based on the specified Stream and character encoding.






Some commonly used write methods of StreamReader


Methods Description
int Read()
This method reads the next character from the input stream as an Int32 object, or -1 if no more characters are available.

int Read(char[] array, int index, int total)
This method reads a character array from the input stream, where:
  • index, represents the starting index in the char array to begin writing at.
  • total, represents the total number of characters read from input stream and write to the char array.
.
This method returns the number of characters that have been read.

String ReadLine()
This method reads a line of characters from the current stream and returns the data as a String, or null if the end of the input stream is reached.

void Close()
This method closes this character stream and also the wrapped byte stream and frees any resources connected with these streams.






In the upcoming example, we are going to read an existing file(which has permissions to read it). Let's say the contents of this file are -

MyFile4.txt
Don't give up on your dreams! Work hard for them!


In this example, we are going create an object of the StreamReader class by wrapping the byte stream class FileStream, to read characters(one character at a time from a file by calling the Read() method of StreamReader class.

We will perform the reading of characters by using the default UTF-8 encoding of StreamReader class.

//C# Example of StreamReader to read characters(one character at a time), from a file.

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

class A
{
public static void Main(String[] ar)
{
	try 
	{
		//Creating an object of FileStream to read to a file
		//We are going to use the FileMode Open to open a new file to read from it
		FileStream fs = new FileStream("D:\\MyFile4.txt", FileMode.Open);

		//Creating an object of StreamReader to read to a file by wrapping the FileStream
		//To read characters from it(one character at a time) 
		StreamReader sr = new StreamReader(fs);

		Console.WriteLine("Reading characters(one character at a time) from a file using StreamReader");

		int c;

		while((c = sr.Read()) != -1) 
		{
		Console.Write((char)c);
		}		

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


Output is -:


Reading characters(one character at a time) from a file using StreamReader
Don't give up on your dreams! Work hard for them!


Program Analysis





Advertisement




  • Reading a line of characters a time by StreamReader with default UTF-8 encoding.


  • In the upcoming example, we are going to read the contents of an existing file(which has permissions to read it). Let's say the contents of this file are -

    MyFile5.txt
    Don't give up on your dreams! Work hard for them!
    Keep Smiling!
    Please, be kind and help whoever you can.


    But this time, we are going to perform a faster reading operation by reading a whole line of characters at a time from a file by calling the ReadLine() method of StreamReader class.

    The ReadLine() method reads a line of characters from the current stream and returns the data as a String. It returns null when the end of input stream is reached and there are no more characters to read.

    We will perform this reading operation by using the default UTF-8 encoding of StreamReader class.

    //C# Example of StreamReader to read characters(one character at a time), from a file.
    
    using System;
    using System.IO;
    using System.Text;
    
    class A
    {
    public static void Main(String[] ar)
    {
    	try 
    	{
    		//Creating an object of FileStream to read to a file
    		//We are going to use the FileMode Open to open a new file to read from it
    		FileStream fs = new FileStream("D:\\MyFile5.txt", FileMode.Open);
    
    		//Creating an object of StreamReader to read to a file by wrapping the FileStream
    		//To read characters from it(one character at a time) 
    		StreamReader sr = new StreamReader(fs);
    
    		Console.WriteLine("Reading a line of characters from a file using StreamReader:");
    
    		String s;
    
    		//Calling the ReadLine() method which returns the String that is read
    		//It returns null if there is nothing to read anymore
    		while((s = sr.ReadLine()) != null) 
    		{
    		Console.WriteLine(s);
    		}		
    
    		//Closing the stream i.e. StreamReader stream
    		sr.Close();
    	}
    	catch(IOException e)
    	{
    		Console.WriteLine(e);
    	}	
    }
    }
    


    Output is -:


    Reading a line of characters from a file using StreamReader:
    Don't give up on your dreams! Work hard for them!
    Keep Smiling!
    Please, be kind and help whoever you can.


    Program Analysis





  • Example of StreamReader with a non-default Unicode encoding.


  • In this example, we are going create an object of the StreamWriter class and use it to write characters to a file, And next, we are going create an object of the StreamReader class and use it to read characters that are previously written by StreamWriter class.

    Note: The writing and reading operation are performed by using the same non-default Unicode encoding.

    //C# Example of StreamReader to read a line of characters using non-default Unicode encoding.
    //This example first uses StreamWriter to write characters using the non-defau.t Unicode encoding.
    //And then, it uses the StreamReader to read a line of characters using the same non-default Unicode encoding.
    
    
    using System;
    using System.IO;
    using System.Text;
    
    class A
    {
    public static void Main(String[] ar)
    {
    	A ob = new A();
    	ob.WriteValues();
    	ob.ReadValues();
    }
    
    public void WriteValues()
    {
    	try
    	{
    		//Creating a char value
    		char ch = 'C';
    
    		//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 FileStream to write to a file
    		//We are going to use the FileMode Create to create a new file 
    		FileStream fs = new FileStream("D:\\MyFile5.txt", FileMode.Create);
    
    
    		//Creating an object of StreamWriter to write characters to a file
    		//By wrapping the FileStream 
    		StreamWriter bw = new StreamWriter(fs, unicode);
    
    		Console.WriteLine("Writing characters to the file using Streamwriter, based on Unicode encoding");
    
    		//writing a char
    		bw.Write(ch);
    
    		//writing a String 
    		bw.Write(str);
    	
    	
    		//Calling the Flush() method to flush the buffered characters to the file
    		bw.Flush();
    
    		//Closing the stream i.e. BinaryWriter stream
    		bw.Close();
    	}
    	catch(IOException e)
    	{
    		Console.WriteLine(e);
    	}
    }
    
    public void ReadValues()
    {
    	try 
    	{
    		//Creating an object of Encoding type
    		Encoding unicode = Encoding.Unicode;
    		
    
    		//Creating an object of FileStream to read to a file
    		//We are going to use the FileMode Open to open a new file to read from it
    		FileStream fs = new FileStream("D:\\MyFile5.txt", FileMode.Open);
    
    		//Creating an object of StreamReader to read to a file by wrapping the FileStream
    		//To read characters from it(one character at a time) 
    		StreamReader sr = new StreamReader(fs, unicode);
    
    		Console.WriteLine("Reading characters from a file using StreamReader, based on Unicode encoding");
    
    		String s;
    
    		//Calling the ReadLine() method which returns the String that is read
    		//It returns null if there is nothing to read anymore
    		while((s = sr.ReadLine()) != null) 
    		{
    		Console.WriteLine(s);
    		}			
    
    		//Closing the stream i.e. StreamReader stream
    		sr.Close();
    	}
    	catch(IOException e)
    	{
    		Console.WriteLine(e);
    	}	
    }
    }
    


    Output is -:


    Writing characters to the file using StreamWriter, based on Unicode encoding
    Reading characters from a file using StreamReader, based on Unicode encoding
    CKeep smiling and think positive.







    Please share this article -




    < Prev
    Next >
    < C# StreamWriter
    C# TextWriter >



    Advertisement

    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