Text Reader and Text Writer in C#


INTRODUCTION:

This article covers the information of how to read or write (Unicode) character based data through TextReader and TextWriter. The TextReader and TextWriter are base classes. The StreamReader and StringReader derives from the abstract type TextReader. Similarly the StreamWriter and StringWriter derives from the abstract type TextWriter.

STREAMWRITERS and STREAMREADERS:

As I mentioned earlier StreamWriter type derives from a base class named TextWriter. This class defines members that allow derived types to write textual data to a given character stream.

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 we have to know about "FileInfo" class. What is FileInfo? In the framework of .NET, the system.IO namespace is the region of the base class libraries devoted to file-based input and output services.File Info is one of the core type of System.IO Namespace.

The function of the FileInfo is to encapsulate a number of details regarding existing files on your hard drive (size,file attributes,creating time,etc.)as well as aid in the creation and destruction of new files. Let us move to example.

In this example in the Writetextfile class I create a file named "Arungg.txt" using the FileInfo class. After creating the text file using the CreateText() method, I get a StreamWriter and write some textual data to the newly created text file. We can also add numeric data into the text file.

public class Writetextfile
{
public static void Main(string[] args)
{
FileInfo t =
new FileInfo("Arungg.txt");
StreamWriter Tex =t.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 can treat textual information as a stream of in-memory characters. In this we 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 a block of 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();
Console.WriteLine("Data: {0}",wr.ToString());
return0;
}
}

Running the above program we get the textual data in the console.

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 character data to a StringWriter type and modify the data by inserting some item to buffer at position 45.After inserting I also show how to remove some particular string from the data.

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 user has gained some information about how to create,write and read in a text file using TextReader and TextWriter in C# and also something about Friendship!.


Similar Articles