| StreamWriter and StreamReader taking twice the memory footprint in some cases over the basic FileStream. StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output.StreamReader is nothing but file pointer.
StreamReader is used to read data from the file.
StreamWriter is used to write data into the file.
StreamReader methods:-Below are the some main methods in streamreader
1)Read() : Reads single character.
2)ReadLine() : Reads single line.
3)ReadToEnd() : Reads full file.
StreamWriter methods:-Below are the some main methods in streamWriter
1)Write()
2)WriteLine()
Example:-Below is example to streamreader text file
using System;
using System.IO;
class Teststreamreader
{
public static void Main()
{
try
{
// create instance of streamreader.
using (StreamReader stread = new StreamReader("FileName.txt"))
{
string strline;
// Read and display lines of file
while ((strline = stread.ReadLine()) != null)
{
Console.WriteLine(strline);
}
}
}
catch (Exception e)
{
Console.WriteLine("Problem with file could not be read:");
Console.WriteLine(e.Message);
}
}
}
Example:-Below is example to streamwriter on text file
using System;
using System.IO;
public class testStearmwriter
{
public static void Main(String[] args)
{
FileStream sb = new FileStream("FileName.txt", FileMode.OpenOrCreate);
char[] b = {'b','b','c','d','e','f','g','h','i','j','k','l','m'};
StreamWriter sw = new StreamWriter(sb);
sw.Write(b, 3, 8);
sw.Close();
}
} | | |