Tuesday, 25 December 2012 13:55

Text reader and writer in csharp

Written by 
Rate this item
(0 votes)

this article covers how to read,write data through
textreader and textwriter.

streamwriters and streamreaders:

the base class TextWriter defines members which
permits derived types to write text content to
a character stream.
some of the main members are follows -
close() which closes the writer and frees resources.
write() writes a line to the text stream
writeLine() writes line to the stream with new line
Flush() which clears all buffers

Writing to text file ::

following example shows writing to text file -

we have to use FileInfo class.this class in the .net
framework is under system.IO namespace(for services
of file based input and output).

here i create a file using the fileinfo class and
after that using SreamWriter,i write some data
in the created file -

public class Writingtotextfile
public static void Main(string[] args)
FileInfo test = new FileInfo("test.txt");
StreamWriter st =test.CreateText();
Console.WriteLine(" The Text file is created ");

Reading from text file ::

now i will show how to read a text file using
StreamReader type.some main members of the class
TextReader follows -
read() which reads data from input stream
ReadLine() reads a line from current stream
and returns content as string
ReadToEnd() reads all content to the end of Text
reader and returns as string.

public class Readfromtextfile
public static int Main(string[] args)
StreamReader test = File.OpenText("test.txt");
string inputs = null;
while ((inputs = test.ReadLine()) != null)
test.close;return 0;

Read 3112 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.