Monday, February 28, 2011

TextReader and TextWriter In C#

TextReader and TextWriter In C#

INTRODUCTION:
This article covers the information of how toread or write (Unicode) character based data through TextReader andTextWriter. The TextReader and TextWriter are base classes. TheStreamReader and StringReader derives from the abstract typeTextReader. Similarly the StreamWriter and StringWriter derives fromthe abstract type TextWriter.
STREAMWRITERS & STREAMREADERS:
As I mentioned earlier StreamWriter typederives from a base class named TextWriter. This class defines membersthat allow derived types to write textual data to a given characterstream.
Let us see some of the main members of the abstract class TextWriter.
1.close() — Closes the Writer and frees any associated resources.
2.Write() — Writes a line to the text stream, with out a newline.
3.WriteLine() — Writes a line to the text stream, with a newline.
4.Flush() — Clears all buffers.

WRITING – TEXT FILE:
Let us see the Writing to a Text File with a example.
Before we move into writing to a text file wehave to know about "FileInfo" class. What is FileInfo? In the frameworkof .NET , the system.IO namespace is the region of the base classlibraries devoted to file-based input and output services.File Info isone of the core type of System.IO Namespace.The function of the FileInfo is to encapsulate a number of detailsregarding existing files on your hard drive (size,fileattributes,creating time,etc.)as well as aid in the creation anddestruction of new files. Let us move to example.
In this example in the Writetextfile class Icreate a file named "Arungg.txt" using the FileInfo class. Aftercreating the text file using the CreateText() method, I get aStreamWriter and write some textual data to the newly created textfile. We can also add numeric data into the text file.
public class Writetextfile
{
public static int Main(sting[] args)
{
FileInfo t = new FileInfo("Arungg.txt");
StreamWriter Tex =f.CreateText();
Tex.WriteLine("Arungg has launced another article");
Tex.WriteLine("csharpheaven is the new url for c-sharpcorner");
Tex.Write(Tex.NewLine);
Tex.close;
Console.WriteLine(" The Text file named Arungg is created ");
}
}

If you open the text file the data is entered there.
READING – TEXT FILE:
Now let us see how to read a text file using the StreamReader type.
Let us see some of the main members of the abstract class TextReader.
1.Read() — Reads data from an input stream.
2.ReadLine() — Reads a line of characters from the current stream and returns the data as a string.
3.ReadToEnd() — Reads all characters to the end of the TextReader and returns them as one string.


public class Readtextfile
{
public static int Main(string[] args)
{
StreamReader re = File.OpenText("Arungg.txt");
string input = null;
while ((input = re.ReadLine()) != null)
{
Console.WriteLine(input);
}
re.close;
return 0;
}
}

In the above class Readtextfile I open the text file Arungg.txt and read the contents using the ReadLine() method.In both StreamReader and StreamWriter are concerned with moving text data to and from a specified file.
STRINGWRITERS & STRINGREADERS:
Using the StringReaders and StringWriters cantreat textual information as a stream of in-memory characters. In thiswe can insert or remove string between a block of textual data.Let us see a program in which I add and delete some string between ablock of text.<pre. wr="new">Running the above program we get the textual data in the console.</pre.>
The output:
Data:
Friendship is not a two way road.
It is a one way road travelled by two people.

Press any key to continue
Now I show you how to insert and delete some string between textual data.
using System.Text;
public class stringwrite
{
public static int Main(string[] args)
{
StringWriter wr = new StringWriter();
wr.WriteLine("Friendship is not a two way road.");
wr.WriteLine("It is a one way road travelled by two people.");
wr.Write(Writer.NewLine);
wr.close();
StringBuilder bu = wr.GetStringBUilder();
string entiredata = bu.ToString();
Console.WriteLine("The data:{\n0}",entiredata);
// TO ADD SOME STRING.
bu.Insert(45,"together-hand in hand");
entiredata = bu.ToString();
Console.WriteLine("The modified data:\n{0}",entiredata);
// TO REMOVE SOME STRING.
bu.Remove(45."together-hand in hand".Length);
entiredata = bu.ToString
Console.WriteLine("The original data:\n{0}",entiredata);
return 0;
}
}

In the above program I write some characterdata to a StringWriter type and modify the data by inserting some itemto buffer at position 45.After inserting I also show how to remove someparticular string from the data.
The Output:
The Data:
Friendship is not a two way road.
It is a one way road travelled by two people.

The modified data:
Friendship is not a two way road.
It is a one way road travelled by two people together-hand in hand.

The original data:
Friendship is not a two way road.
It is a one way road travelled by two people.

Now Let us see how to use StringReader to read a block of character data rather than a entire file.
StringReader re = new StringReader(wr.ToString());
string input = null;
while(( input = re.ReadLine()) != null)
{
Console.Write\line(input);
}
re.close();

Like the above way by using StringReader we can read data from a file.
CONCLUSION:
I hope after reading this article , the userhas gained some information about how to create,write and read in atext file using TextReader and TextWriter in C# and also somethingabout Friendship!

No comments:

Post a Comment