Advertisement



< Prev
Next >



C# TextWriter



C# provides us the TextWriter abstract writer class which allows us to write a sequential series of characters the stream. The TextWriter abstract class is implemented by the StreamReader and StringReader class.




Some commonly used write methods of TextWriter


Methods Description
void Write(int value)
This method writes a signed int value to the stream.

void Write(uint s)
This method writes an unsigned int value to the stream.

void Write(Single f)
This method writes a float to the stream.

void Write(double d)
This method writes a double to the stream.

void Write(long l)
This method writes a long to the stream.

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

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 WriteLine(int value)
This method writes a signed int value to the stream.

void WriteLine(uint s)
This method writes an unsigned int value to the stream.

void WriteLine(Single f)
This method writes a float to the stream.

void WriteLine(double d)
This method writes a double to the stream.

void WriteLine(long l)
This method writes a long to the stream.

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

void WriteLine(char ch)
This method writes a character value to the stream.

void WriteLine(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 WriteLine(String str)
This method writes a String to the stream.

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

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






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.

//C# Example of TextWriter


using System;
using System.IO;

class A
{
public static void Main(String[] ar)
{
	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 tr = File.CreateText("D:\\MyFile10.txt");

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

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

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

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


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

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

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

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

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


Output is -:


Using the TextWriter to write to a  file
Data written successfully


The program creates a file(MyFile10.txt") and writes its contents, which are shown below contents -:

-109C89.9875.88810000000000Keep smiling and think positive.

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.


Advertisement




  • Example of TextWriter.


  • 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 WriteLine() method.

    //C# Example of TextWriter
    
    
    using System;
    using System.IO;
    
    class A
    {
    public static void Main(String[] ar)
    {
    	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 tr = File.CreateText("D:\\MyFile11.txt");
    
    		Console.WriteLine("Using the TextWriter to write to a  file");
    
    		//writing an int 
    		tr.WriteLine(i);
    
    		//writing an uint 
    		tr.WriteLine(ui);
    
    		//writing a char
    		tr.WriteLine(ch);
    
    
    		//writing a float 
    		tr.WriteLine(f);
    
    		//writing a double
    		tr.WriteLine(d);
    
    		//writing a long
    		tr.WriteLine(l);
    
    		//writing a String 
    		tr.WriteLine(str);
    		
    		
    		Console.WriteLine("Data written successfully");
    	
    	
    		//Calling the Flush() method to flush the buffered data to the file
    		tr.Flush();
    
    		//Closing the stream i.e. BinaryWriter stream
    		tr.Close();
    	}
    	catch(IOException e)
    	{
    		Console.WriteLine(e);
    	}
    }
    }
    


    Output is -:


    Using the TextWriter to write to a  file
    Data written successfully


    The program creates a fileMyFile11.txt" and writes its contents by using the versions of WriteLine method of TextWriter class. The contents of the file are shown below contents -:

    -109C89.9875.88810000000000Keep smiling and think positive.

    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.




    Please share this article -




    < Prev
    Next >
    < C# StreamReader
    C# TextReader >



    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