StreamReader and StreamWriter
To handle text streams more easily, the .NET language has the StreamReader and StreamWriter classes. But these class do NOT inherit from Stream, they encapsulate it.
The StreamReader and StreamWriter classes derive respectively from TextReader and TextWriter which are designed to ease reading or writing text oriented streams.
Just like Stream, TextReader and TextWriter are abstract classes which exposes functionality that are implemented by it's inheritor. As all class implementing TextReader or TextWriter behave in the same way, we will consentrate on StreamReader and StreamWriter.
You can instanciate a StreamReader or a StreamWriter by passing a file name or a Stream to the constructor. Due to the fact that these classes are specially designed to handle texts, you can also construct an instance by passing it an encoding. By default, StreamReader and StreamWriter use the unicode encoding.
The following tables describe the properties and methods of those classes:
StreamReader | |
---|---|
Object -> TextReader -> StreamReader | |
using System.IO; | |
Methods | |
Prototype | Description |
int Peek() | Return the next caracter in the stream. Return -1 if no more caracter available. |
int Read () | Same as Peek but DO remove the caracter from the stream. |
string ReadLine() | Read the next line. A line is built of caracters and is ended by \n or \r. ReadLine return null if no more data is available |
string ReadToEnd() | Read the whole file |
StreamWriter | ||
---|---|---|
Object -> TextWriter -> StreamWriter | ||
using System.IO; | ||
Properties | ||
Name | Type | Description |
AutoFlush | bool | In the case of a bufferized I/O, if AutoFlush is true, a write will be automatically affect the stream, not only the buffer |
NewLine | string | Caracters of line ending. By default those caracters are \r\n |
Methods | ||
Prototype | Description | |
void Flush() | Force writing on the stream | |
void Write( args ) | Write in the stream. args may be of any type. | |
void WriteLine( args ) | Perform the same operation as Write but a new line is appended to the stream. This new line correspond to the string specified for the NewLine property. |
The following example is divided in two parts. In the first part, a file is read using FileStream's methods; in the second part, the same file is read using a StreamReader
using System.IO;
using System.Text;
namespace StreamTest
{
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// 1°) Reading a text file using FileStream
FileStream fs = new FileStream("c:\\test.txt", FileMode.Open);
byte[] data = new byte[fs.Length];
fs.Read(data,0,(int)fs.Length);
fs.Close();
string fileContent = Encoding.ASCII.GetString(data);
Console.Write(fileContent);
Console.ReadLine();
// 2°) Readind a text file using StreamReader
FileStream fs2 = new FileStream("c:\\test.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs2, Encoding.ASCII);
string fileContent2 = sr.ReadToEnd();
Console.Write(fileContent2);
Console.ReadLine();
}
}
}
Advantages | Drawbacks |
---|---|
Easy text file access | Do not inherit from Stream |
Easy encoding management through Encoding class | |
Can be constructed around any Stream object, thus allowing binary reading/writing on any streaming source |
Related articles: