C# Beginner to advanced - Lesson 60 - Stream reader and stream writer (System.IO)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to lesson sixty stream input and output brought to you back pro technologies my name is Harish in this session we are going to learn what is stream input and output operations right now we understand what is the what is stream first c-sharp includes following standard input and output classes to read or write from different resources like file memory Network and isolated storage in the sense if you want to write something to the file which is stored in the folder then Mustang should we should make use of this link class to do that work and let us understand what stream is first you can observe your system dot IO dot stream it is an abstract class that provides standard methods to transfer bytes to the source in the sense if you want to read or if you want to write something to the file then must and should we should make of this stream class and keep in mind that stream class is present in system dot IO namespace ok classes that need to read and write bytes from a particular source must implements the stream class in the sense you can observe your the following classes in there in stream class to provide functionality to read and write bytes from a particular source for example we have a file stream class your ok and what it does is it reads or bytes writes bytes from or to a physical file whether it is a txt X Y or jpg or any other file file stream is derived from the stream class in the sense Donna said it very clearly I'm Emily image which explains that for example as I said stream class helps you to read and write to a physical file and it is having two methods called stream reader and stream writer okay now let us understand file stream first what is this file stream now if I if I want to write something to this physical file then using this dream writer what I will do is I will pass that string I'll write the string and that string will be converted into bytes with the help of stream writer and that bytes will be passed to file stream and file stream will take that bytes and it writes into the physical file okay this is how the stream writer works and coming back the stream reader if if someone wants to read something from this file okay then the file stream reads the content which is present in this file and it convert into bytes later that bytes are are given to stream reader stream reader converts that string that's that bytes into string okay this is how the stream class works and also the file stream class works right now we'll jump back into the slide where we left ok this is how the stream class and the file stream class your work and now we have some other classes like memory stream buffer stream if you want to read something or write something to the memory part or a buffered stream part then we should make use of this class okay and network stream pipe dream and crypto stream these are the classes which is there in stream class to do the read and write operations and here is the clear hierarchy as can you observe you can observe of a stream class like a stream class is inherited to these respective classes like Krypton's free memory stream file stream buffered has little storage and pipedream to do the read and write operations ok and now let us understand we understand what is stream class it is an abstract class which helps us to read and write to a respective file memory stream or whatever it is etc and that string class consists of certain methods like stream reader stream writer binary reader and by narrator what is the stream reader like stream reader is a helper class for reading characters from string by converting bytes into characters using an encoded value it can be used to read strings for member from different stream like file stream and memory stream okay now streamwriter it also writes on the physical file similarly the binary reader is L per class for reading primitive data type from bytes binary writer binary writer writes primitive types into binary now we are going to concentrate on only these two things like stream reader and stream writer and your is image as I explained earlier now once we code later we'll come into this image and once again X I'll explain how it works now without wasting much time let's invoke the visual studio now what I what is my objective is for a map in in the drive called the drive I'm having a folder called read and write I'm having a dot txt file let me open this currently I have written something called hello world and let me open it again currently nothing is there in this dot txt file now what shall we do is with the help of stream writer let me write something to this to this txt file demo dot txt file now what shall we do is now first thing we have to do is let me create two methods okay one method is going to write and other method is going to read public void right for file the method name is okay and let me create another method like read public void read for file is the method name okay and now let us do with the right right for file first okay how do I write something to the file it is with the help of stream writer let me call that stream writer first okay now it is showing a red squiggly because that is not present your namespace is not present of the particular class let me add the namespace you can observe the stream writer is present in using system dot IO when I added it the red squiggly is no more there it's gone let me create an object of the class let me give it as a writer is equal to new stream writer and here I have to give the file path you can observe it is having the eight variations and it's asking the file path let me give the file path of it where do I store that I've stored in the folder you know let me copy the address of it okay we'll see and let me paste it here and the file name was demo dot txt let me add that demo dot txt and this is the file path I have to add at at the starting okay then only it will be considered as the file path okay now a writer okay now I've created our object of the stream writer using that object let me access the methods called right in observe right line now what I have what I want to write to this particular file is let me write a string called hello world okay I'm done I did it and now let me close that method let me close the string stream writer there is a method called close okay now what did i do I just wrote I just wrote called a low world string to the file called demo dot txt now let me build it okay the build succeeded the build is started and it gets succeeded okay done now what did i do I just wrote something to the file now let me go to the read for file okay read for file now if I want to read something this other world from this demo dot txt what I do is I have to invoke a class called I have to call it class called stream reader and it's present in system dot IO namespace let me give the object name as reader is equal to new stream reader okay for this also I have to get the file path so what I'll do is I'll just copy and I'll paste it here control V and reader dot if I want to read then I am read to end method which reads complete which reads the complete document okay then reader dot let me close that let me close once it is dried let me close that method let me close that finalized that object okay now what did I do is you can observe very clearly I am having two methods public wide write for file and public void read for file and that's what we did it here with help of stream writer and stream reader now what I have to do is I have to invoke these two methods and confirm whether the stream writer has written hello well to that demo dot txt and I have two other and I have two can also confirm whether it is reading from that file so what I will do is in order to call this file let me make this file state attic first static and you're also let me make static okay now let me call the first let me let me write something to the file then read it okay now let me call the right for file okay and let me call the read for file method okay now in nod read what I have to do is I have to pass it to the console dot write line control X and let me paste with your control V okay my job is done what did I do is with the help of stream writer and stream reader I wrote a low world on to the text on the demo dot text and I am reading it using the stream reader now when I run this program control fi what is doing it's it's printing a low world in the sense it has written a low well to the demo dot txt file then it has read that hello world now to confirm let us go back to the folder where I've stored that file demo dot txt if I open this txt file there should be a string called hello world in the sense yes our stream class has done its work okay and that's what we wanted and that's what we learnt in this session we wrote two methods one is for write another fun for reader with help of stream writer and stream reader we did the we did we write we wrote and we read it from we read it with help of stream classes stream class okay and now jumping back to presentation your eye image okay now in the presentation we may we wrote the hello world to the file demo dot txt LS understand with help of image here in streamwriter class what I do I just invoke the I just created object to that object I passed the file path later using the right line method I wrote a low world okay when when I write something a low world what is the stream writer class is going to do is it converts that string to bytes and it give it and it gives to the file stream file stream later right writes that bytes into the physical file that's what exactly happens okay now it's written hello well to this physical file now if I want to read read it back or I have to do is I have to make use of the streamreader what is the stream reader is going to do first okay this files file stream is going to get that bytes that ello world in the form of bytes and is converting into string okay once it is converting into string it is taken by a stream reader and list read on the console screen and that's what exactly works in the stream class and that's the beauty of stream class and the beauty of these two methods also stream reader and stream writer and that's what we learn in this session what is stream class and what are the methods streamwriter and stream writer and what is file stream and finally thank you for listening have a great day please subscribe tranquil training below and don't forget to give the feedback
Info
Channel: Ankpro Training
Views: 35,280
Rating: undefined out of 5
Keywords: tips and tricks, ankpro, ankpro training, C#, C sharp, stream reader, stream writer, stream reader in csharp, stream reader in c#, stream writer in csharp, stream writer in c#, memory stream, buffer stream, file stream, FileStream, BufferedStream, network stream, pipe stream, crypto stream, binary reader, binary writer, primitive data types, system.io, system.io namespace, stream read and write, stream class, System.IO.Stream, standard IO, transfer bytes, io operations
Id: CN5A3Q2ePak
Channel Id: undefined
Length: 11min 51sec (711 seconds)
Published: Mon Jun 06 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.