Stream is a flow of data between files or objects or sockets or strings or other streams. Efficiently we transfer data between 2 places. all Streams are available in System.IO namespace.
we have more streams available in .net.
like Filestream,Memorysteam,Streamwrtier,StreamReader,stringwriter,stringReader,BinaryReader,BinnaryWriter.
..ect and Each stream has specific purpose.
How many types of streams in dotnet?
yes, we have 3 types of streams in .net
1. Byte based streams 2. charater based streams, 3. primitive based streams.
1.Byte based streams:
what is byte based streams? stream of data is availble in the form of bytes(an array of bytes).
Stream is base Class for these type of streams.
Stream(base class)
--->canRead,CanSeek,CanTimeout,Close,Flush.....ect are defined in this stream class.
what the streams accepts data in the form of bytes(byte[] ) array are called byte based streams.
Ex FileStream, MemoryStream
2.Character based Streams.
TextReader,TextWriter are base classes for character based streams.
TextReader
-----Read,ReadLine,ReadToEnd...ect
TextWriter
-----write,writeLine,close,Flush......ect
Ex:stringwriter,StringReader,StreamReader,StreamWriter....ect
Ex:
FileStream: accepts data in the form of an array of bytes.
// open a Filstream to the source file
FileStream fin = new FileStream(@"F:\sample1.txt", FileMode.Open, FileAccess.ReadWrite);
//Open a FileStream to the destination file
FileStream fout = new FileStream(@"F:\sample10.txt", FileMode.OpenOrCreate,FileAccess.Write, FileShare.ReadWrite);
//Create a byte array to act as a buffer
Byte[] buffer = new Byte[32];
Console.WriteLine("File Copy Started");
//Loop until end of file is not reached
while (fin.Position != fin.Length)
{
//Read from the source file
//The Read method returns the number of bytes read
int n = fin.Read(buffer, 0, buffer.Length);
//Write the contents of the buffer to the destination file
fout.Write(buffer, 0, n);
}
//Flush the contents of the buffer to the file
// fout.Flush();
//Close the streams and free the resources
fin.Close();
fout.Close();
Console.WriteLine("File Copy Ended");
Console.ReadKey();
between these 2 streams fin,fout the data should be travelled in the form of bytes of array.
From Source File to Destination File. data in the sample1.txt travelled to sample2.txt in the form of byte array.
and copied entire data to into sample2.txt file.
2. Character based Streams.
StreamReader and StreamWriter are used to read the text file data in the stream of character based format.
Data between the 2 files or 2 object done by reading or wrirting characters one by one.
Ex1:
StreamReader sr = new StreamReader(@"D:\test1.txt");
Console.WriteLine(sr.ReadToEnd());
Console.ReadKey();
or
FileInfo finfo = new FileInfo("D:\\test1.txt");
StreamReader sr1=finfo.OpenText(); // File.Open("C:\abcd.txt,FileMode.Wrtie);
Console.WriteLine(sr1.ReadToEnd());
Or
StreamReader sr1 = new StreamReader("F:\\sample.txt");
Console.WriteLine(sr1.ReadToEnd());
here whatever data available in test1.txt is displayed in console window.
Ex2:
StreamWriter sw1 = new StreamWriter("D:\\kasibabu.txt");
sw1.Write("akdfjaklsd");
sw1.WriteLine();
sw1.WriteLine("---");
sw1.WriteLine("end");
sw1.Flush();
sw1.Close();
Ex3:
FileInfo finfo = new FileInfo(@"F:\stream10.txt");
StreamWriter sw = finfo.CreateText();
sw.Write("dfasdfasdf");
sw.WriteLine("aaaaaaaaaaaaaaaa");
sw.Write("kasibabu");
sw.Flush();
sw.Close();
StreamReader sr = finfo.OpenText();
while (sr.Peek() > -1)
{
Console.WriteLine(sr.ReadLine());
}
Console.ReadKey();
StringWriter and StringReader are in-memory based strings.which takes stringbuilder as a argument to perfom the input and output stream operations
StringWriter and StringReader:
StringBuilder sb=new StringBuilder();
sb.Append("kasi");
sb.AppendLine();
sb.Append("babu");
StringWriter sw = new StringWriter(sb);
Console.WriteLine(sw);
No comments:
Post a Comment