Thursday, 22 March 2012

How to Use Streams,StreamWriter,StreamReader?

What  is   stream?
a stream is a general representation of bytes. We use a stream-based classes to read bytes of data from sources like files, memory or even network locations.

As per the source we have to choose the stream classes like FileStream,Memorystream,Networkstream….ect
Right now I am working with files means my source is File so choose FileStream.
Using File class I can read the data and write the data using Files.

FileStream is a stream based type which is derived from Stream abastract class.

File using stream based classes
Ex:
  FileStream  fss=File.Create("C:\\abcdd.txt");
            
   fss.WriteByte(98);
File Class has
public static StreamWriter CreateText(string path);
public static StreamReader OpenText(string path);
static void Main()
{
            string strpath=@"C:\\zolt.txt";
            StreamWriter w = File.CreateText(strpath);
            w.WriteLine("This is from");
            w.WriteLine("Chapter 6");
            w.WriteLine("Of C# Module");
            w.Write(w.NewLine);
            w.WriteLine("Thanks for your time");
            w.Close();
            Console.WriteLine("=========");
            StreamReader s = File.OpenText(strpath);
                string read = null;
                while ((read = s.ReadLine()) != null)
                {
                  Console.WriteLine(read);
                }
                s.Close();                       
            Console.Read();
  }
FileInfo Class
            FileInfo finfo = new FileInfo(@"C:\aa.txt");
            StreamWriter  w=finfo.AppendText();  // CreateText();
            w.WriteLine("This is from");
            w.WriteLine("Chapter 6");
            w.WriteLine("Of C# Module");
            w.Write(w.NewLine);
            w.WriteLine("Thanks for your time");
            w.Close();
CreateText() methods create every time override the File.

AppendText() method append the data to existing data. 
Working With StreamWriter and StreamReader classes?

StreamWriter and StreamReader are sealed Classes which derived from TextWriter and TextReader Classes.

TextReader and TextWriter for Text data.
like
XmlTextReader,xmltextWriter.
StreamWriter:
             string strpath = @"C:\\zolt.txt";

StreamWriter w = new StreamWriter(strpath);
            w.WriteLine("This is from");
            w.WriteLine("Chapter 6 kasibabu");
            w.WriteLine("Of C# Module");
            w.Write(w.NewLine);
            w.WriteLine("Thanks for your time");
            w.Close();
            Console.WriteLine("+++++++++++");
 StreamReader:
                StreamReader sr = new StreamReader(strpath);          
                String line;
                // Read line by line
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
Another way is

StreamWriter and  TextWriter
            string strpath = @"C:\\zolt.txt";
            TextWriter writeFile = new StreamWriter(strpath);
            writeFile.Write("dalksfjdflaksj");
            writeFile.Write(10);
            writeFile.Write("daflsfkasibabu");
            writeFile.Flush();
StreamReader and TextReader
            string line1 = null;
            System.IO.TextReader readFile = new StreamReader("C:\\dfgsdf.txt");
            line1 = readFile.ReadToEnd();
            Console.WriteLine(line1);           
            readFile.Close();
            readFile = null;         

Streamwriter  and StreamReder can take any stream as a parameter to write and read.
StreamWriter accepts stream as paramter

public StreamWriter(Stream stream);
public StreamReader(Stream stream); 

Ex:
       FileStream  fs=File.Create("C:\\naveen.txt");
            StreamWriter sw = new StreamWriter(fs);
            sw.Write("i ");
            sw.Write("kasi");
            sw.Write("babu");
            sw.WriteLine("");
            sw.Write("addresss:");
            sw.Flush();
            sw.Close();
StreamReader  accepts stream as paramter

            FileStream fs1 = File.Open(strpath, FileMode.Open);
            StreamReader sr = new StreamReader(fs1);
            string str = sr.ReadToEnd();
            Console.WriteLine(str);
            sr.Close();

When we serialize and deserialize any object then we are then these are very useful…

No comments:

Post a Comment