 Hello everyone and welcome to the session on stream writer and stream reader classes of csharp.net. At the end of the session, student will be able to implement csharp.net programs to perform various operations on files using stream writer and stream reader classes. The system.io namespace provides types for reading encoded characters from streams and writing them to streams. Streams are designed for byte input and output. Each reader and writer class is associated with a stream. Stream reader and stream writer classes are used for reading and writing characters by using an encoding value to convert the characters to and from bytes. First we'll see stream writer class. Csharp.net implements a text writer for writing characters to a stream in a particular encoding. Encoding is the process of transforming a set of unicode characters into a sequence of bytes and decoding is the process of transforming a sequence of encoded bytes into a set of unicode characters. The unicode standard assigns a code point i.e. a number to each character in every supported script. A unicode transformation format i.e. UTF is a way to encode that code point. The unicode standard uses the UTF such as UTF-8, UTF-16, UTF-32. UTF-8 represents each code point as a sequence of 1 to 4 bytes. UTF-16 represents each code point as a sequence of 1 to 2 16-bit integers. UTF-32 represents each code point as a 32-bit integer. Stream writer constructors initialize a new instance of the stream writer class. Have a look at various overloaded constructors of stream writer class shown here. Now let's see the methods of stream writer class. Here are various overloaded versions of write method. We'll see few of them. Write method with care parameter writes a character to the stream. Write method with character array as a parameter writes a character array to the stream. Write method where the parameter is 8-byte floating point value writes the text representation of 8-byte floating point value to the text stream. Write method where parameter is 4-byte signed integer writes the text representation of 4-byte signed integer to the text stream. Write method with object parameter writes the text representation of an object to the text stream by calling the two-stream method on that object. Write line method with string parameter writes a string to the stream followed by a line terminator. Write line method with object parameter writes the text representation of an object to the text stream by calling two-stream method on that object followed by a line terminator. Close method closes the current stream writer object and the underlying stream. Dispose method releases all resources used by text writer object. Now we will see stream reader class. Stream reader constructors initialize a new instance of the stream reader class for the specified stream. Have a look at various overloaded constructors of stream reader class shown here. Now we will see few methods of stream reader class. First method is peak method. It returns the next available character that is returns an integer representing the next character to be read or minus one if there are no characters to be read or if the stream does not support seeking. Next method is read. It reads the next character or the next set of characters from the input stream. There are three overloaded read methods. The read method without parameter reads the next character from the input stream and advances the character position by one character. Read method returns the next character from the input stream represented as in 32 object or minus one if no more characters are available. The next read method accepts one parameter that is buffer. It reads characters from the current stream into a span. It returns an integer that is the number of characters that have been read or zero if end of the stream is reached and no data was read. The next read method has three parameters buffer, index and count. It reads a specified maximum of characters from the current stream to the buffer beginning at the specified index. The second parameter index specifies the index of the buffer at which to begin writing. The third parameter count is the maximum number of characters to read. The next method is read line method. It reads a line of characters from the current stream and returns the data as a string. Next method is close. It closes the stream reader object and the underline stream and releases any system resources associated with the reader. Dispose method closes the underline stream, releases the unmanaged resources used by the stream reader and optionally releases managed resources. The most common types of unmanaged resources are objects that wrap operating system resources such as files, windows, network connections or database connections etc. Dispose method accepts Boolean parameter if true releases both managed and unmanaged resources. If it is false it releases only unmanaged resources. Now pause the video and find the answer. Pick override int peak is a method of dash class. a stream reader b stream writer The answer is a stream reader. Peak is a method of stream reader class. It returns the next available character but does not consume it. Now let's see a program using stream writer and stream reader classes. Here in this example we have used directory info class, stream writer class, stream reader class so we have used namespace system.io. Here we have created object of directory info class and here we are getting the directories of c drive and all the directories from c drive are stored in cdir that is array of directory info type. And then here we have created object of stream writer and a file is created c drive directories dot txt and here we are iterating through all the directory names from this array. Every directory name from this array cdir will be written to the stream. Here we have used write line method of stream writer and here we have used dir dot name so that the directories name will be written to the file. Here we have used a stream variable that is line and here we are reading from the file so we have used stream reader to read from this file and we have used read line method of stream reader and the red line will be stored in variable line and if this is not null then every line will be displayed on console and this is repeated for all the lines from the file and then we have used console dot read key. Let's build the code I will execute it so all these are the directories from c drive. Let's see where the file is created. This is the file which stores all the directory names from c drive. In this session we have seen stream writer and stream reader classes with example. Thank you.