Advertisement



< Prev
Next >



C# TextReader



In our last tutorial, we have explained how you to use an implementation of TextWriter abstract writer class, which allows us to write a sequential series of characters the stream. The TextWriter abstract class is implemented by the StreamWriter and StringWriter class.

In this tutorial, we are going to explain how to use an implementation of TextReader class, which is used to read a sequential series of characters that were previously written by the TextWriter class.

Note: The TextReader abstract class is implemented by the StreamWriter and StringWriter class, which allows us to read from a stream.




Some commonly used methods of TextReader


Methods Description
int Read()
This method reads the next character from the text reader 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 text reader, where:
  • index, represents the starting index in the char array to begin writing at.
  • total, represents the total number of characters read from input string and write to the char array.
.
This method returns the number of characters that have been read from the input string.

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

void Close()
This method closes the TextReader.






In this example, we are going to use an implementation of the TextWriter class and use it to write a sequence of int, character, float, double, long and a String to a file by using the various overloaded version of Write() method.

Next, we are going to an implementation of TextReader class, which is used to read a sequential series of characters(one character at a time) that were previously written by the TextWriter class, by calling its ReadLine() method.

//C# Example of TextReader


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 variables with primitives values
		int i = -10;
		uint ui = 9;
		char ch = 'C';
		float f = 89.9f;
		double d = 875.888;
		long l = 10000000000;
			
		//Creating a String object
		String str = "Keep smiling and think positive.";
	

		//Calling the CreateText() method of the File class to create a new file and
		//it returns an object of a type
		//Which has implemented the TextWriter abstract class.
		TextWriter tw = File.CreateText("D:\\MyFile10.txt");

		Console.WriteLine("Using the TextWriter to write to a  file");

		//writing an int 
		tw.Write(i);

		//writing an uint 
		tw.Write(ui);

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


		//writing a float 
		tw.Write(f);

		//writing a double
		tw.Write(d);

		//writing a long
		tw.Write(l);

		//writing a String 
		tw.Write(str);
		
		
		Console.WriteLine("Data written successfully");
	
	
		//Calling the Flush() method to flush the buffered data to the file
		tw.Flush();

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

public void ReadValues()
{
	try 
	{
		//Calling the OpenText() method of the File class to read an existing file and
		//it returns an object of a type
		//Which has implemented the TextWriter abstract class.
		TextReader tr = File.OpenText("D:\\MyFile10.txt");

		int c;

		Console.WriteLine("Reading the characters by using the TextReader:");
		while((c = tr.Read()) != -1) 
		{
		Console.Write((char)c);
		}		

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


Output is -:


Using the TextWriter to write to a  file
Data written successfully
Reading values from a file using BinaryStream in memory
Reading the characters by using the TextReader:
-109C89.9875.88810000000000Keep smiling and think positive.


The program creates a file(MyFile10.txt) and writes its contents by using the TextWirter. The contents of the file are -:
-109C89.9875.88810000000000Keep smiling and think positive.

The sequence of characters in a file were read(a whole line of characters at once) by calling the ReadLine() method of TextReader.

As you can see, the characters are written to the file using the implementation of TextWriter class, returned by calling the CreateText() method of the File class and the characters are read from the file using the implementation of TextReader class, returned by calling the OpenText() method of the File class.

Advertisement




  • Reading a line of characters using TextReader


  • In this example, we are going to use an implementation of the TextWriter class and use it to write a sequence of int, character, float, double, long and a String to a file by using the various overloaded version of Write() method.

    Next, we are going to an implementation of TextReader class, which is used to read a sequential series of characters(a line of characters at a time), that were previously written by the TextWriter class, by calling its Read() method.

    //C# Example of TextReader
    
    
    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 variables with primitives values
    		int i = -10;
    		uint ui = 9;
    		char ch = 'C';
    		float f = 89.9f;
    		double d = 875.888;
    		long l = 10000000000;
    			
    		//Creating a String object
    		String str = "Keep smiling and think positive.";
    	
    
    		//Calling the CreateText() method of the File class to create a new file and
    		//it returns an object of a type
    		//Which has implemented the TextWriter abstract class.
    		TextWriter tw = File.CreateText("D:\\MyFile11.txt");
    
    		Console.WriteLine("Using the TextWriter to write to a  file");
    
    		//writing an int 
    		tw.Write(i);
    
    		//writing an uint 
    		tw.Write(ui);
    
    		//writing a char
    		tw.Write(ch);
    
    
    		//writing a float 
    		tw.Write(f);
    
    		//writing a double
    		tw.Write(d);
    
    		//writing a long
    		tw.Write(l);
    
    		//writing a String 
    		tw.Write(str);
    		
    		
    		Console.WriteLine("Data written successfully");
    	
    	
    		//Calling the Flush() method to flush the buffered data to the file
    		tw.Flush();
    
    		//Closing the stream i.e. BinaryWriter stream
    		tw.Close();
    	}
    	catch(IOException e)
    	{
    		Console.WriteLine(e);
    	}
    }
    
    public void ReadValues()
    {
    	try 
    	{
    		//Calling the OpenText() method of the File class to read an existing file and
    		//it returns an object of a type
    		//Which has implemented the TextWriter abstract class.
    		TextReader tr = File.OpenText("D:\\MyFile11.txt");
    
    		Console.WriteLine("Reading a line of characters by using TextReader:");
    
    		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 = tr.ReadLine()) != null) 
    		{
    			Console.WriteLine(s);
    		}		
    
    		//Closing the stream i.e. TextReader stream
    		tr.Close();
    	}
    	catch(IOException e)
    	{
    		Console.WriteLine(e);
    	}	
    }
    }
    


    Output is -:


    Using the TextWriter to write to a  file
    Data written successfully
    Reading a line of characters by using TextReader:
    -109C89.9875.88810000000000Keep smiling and think positive.


    The program creates a file(MyFile10.txt) and writes its contents by using the TextWriter. The contents of the file are -:
    -109C89.9875.88810000000000Keep smiling and think positive.

    The sequence of characters in a file were read(a whole line of characters at once) by calling the ReadLine() method of TextReader.




    Please share this article -




    < Prev
    Next >
    < C# TextWriter
    C# StringWriter >



    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