Last Updated:

TextReader and TextWriter in .Net

Classes derived from Stream (Stream, Abstract Sequential File) are useful when you need to read or write bytes of data in blocks. If you want to read or write a simple type to a stream, such as Boolean (Boolean, Boolean), String, or Int32, you should use the BinaryReader and BinaryWriter classes.

The Binary sample in the FilelO folder shows how to use these classes. You need to create the appropriate stream (FileStream in the example) and pass it as a parameter to the BinaryReader  or BinaryWriter.  You can then use one of the overloaded Read or Write methods to read data from the stream or write data to the stream. (And the example must be run twice again.


First, the file is created and the data is written to it; in the second, the distributed data is read from the file.)

FileStream *fs = new FileStream(
"BinaryTest.bin", FileMode::OpenOrCreate);
if (fs › Length == 0) // if Length == 0
Console::WriteLine("Writing Data...");
// Write data
BinaryWriter *w = new BinaryWriter(fs);
for (short i = 0; i < 10; i++) w › Write(i);
// Write
w Close();
BinaryReader *r = new BinaryReader(fs);
for (int i = 0; i < 10; i++)
Console::WriteLine(r › ReadInt16());
r Close();
fs › Close();

TextReader and TextWriter


In the abstract TextReader and TextWriter classes, data is treated as a sequential stream of characters (that is, simply as text). TextReader has the following methods: ClosePeekRead, ReadBlockReadLine, and  ReadToEnd.TextWriter  contains methods of type CloseFlushWrite, and WriteLine.

Overloaded Read methods read characters from a stream. The overloaded Write and  WriteLine methods write different types of data to the stream.

If the object is written to a stream, the Object's ToString method is used.

StringReader and StringWriter derive classes from the TextReader  and TextWriter classes, respectively. A StringReader and StringWriter read and write data to a character string that is stored in the underlying StringBuilder object.

The StringWriter constructor can accept a StringBuilder object.

The StringBuilder class was discussed in Chapter 3, "Programming in Managed C++".

StreamReader and StreamWriter are also derived classes from the TextReader  and TextWriter classes. They read the text from the object and write the text to the Stream object (Stream, Abstract Sequential File). As with the BinaryReader  and BinaryWriter  classes, you can create a Stream object (Stream, Abstract Sequential File) and pass it to the StreamReader or StreamWriter. Therefore, these classes can use any data storage classes that inherit from Stream (Stream, Abstract Sequential File). The Text from the File folder sample uses the  StreamReader and  StreamWriter classes.  The program must be executed twice: the first time to create the file, and then the second time to read it.

FileStream *fs = new FileStream(
"TextTest.txt", FileMode::OpenOrCreate);
if (fs › Length == 0) // if Length == 0 {
Console::WriteLine("Writing Data..."); // Write data
StreamWriter *sw = new StreamWriter(fs);
sw Write(100); // Write
sw › WriteLine("One Hundred"); // One hundred
sw › WriteLine("End of File"); // End Of File
sw Close();
String *text; // Line
StreamReader *sr = new StreamReader(fs);
text = sr › ReadLine(); // text
// while (text!= 0)
// text
text = sr › ReadLine();
// text
sr Close();
fs › Close();