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.>

Continues…

Pages: 1 2

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

One Response to “TextReader and TextWriter In C#”

  1. Very Helpfull info thanks