 Hello everyone and welcome to today's session. Today we'll see two classes, Binary Reader and Binary Writer from Csharp.NET. At the end of this session, student will be able to implement Csharp.NET program to perform various operations to files using Binary Reader and Binary Writer classes. We'll begin with Binary Reader class. Binary Reader class reads primitive data types as binary values in a specific encoding. The Binary Reader class provides methods that simplify reading primitive data types from a stream. For example, we can use readBoolean method to read the next byte as a Boolean value and advance the current position in the stream by one byte. The class includes read methods that support different data types. When we create a new instance of the Binary Reader class, we provide a stream to read from and optionally specify the type of encoding and whether to leave the stream open after disposing the Binary Reader object. If we do not specify an encoding type, UTF8 is used. There are three constructors of Binary Reader class. The syntax of constructors is shown here. The first constructor takes one parameter that is the input stream. It initializes a new instance of the Binary Reader class based on the specified stream. The next constructor takes two parameters that is the input stream and the character encoding to use. It initializes it initializes a new instance of the Binary Reader class based on the specified stream and character encoding. The third constructor initializes a new instance of the Binary Reader class based on the specified stream, character encoding and third parameter is of type Boolean. It is used to optionally leave the stream open. It is true to leave the stream open after the Binary Reader object is disposed, otherwise false. Now we will see the commonly used methods of Binary Reader class. Close method, it closes the current reader and the underlying stream. Dispose method releases all resources used by the current instance of the Binary Reader class. Read Boolean method returns Boolean. It reads a Boolean value from the current stream and advances the current position of the stream by one byte. Read Care method returns character. This method reads the next character from the current stream and advances the current position of the stream in accordance with the encoding used and the specific character being read from the stream. Next method is read double. This method returns double. It reads an 8 byte floating point value from the current stream and advances the current position of the stream by 8 bytes. Next method is read in 32 method. It returns integer. It reads a 4 byte signed integer from the current stream and advances the current position of the stream by 4 bytes. Next method is read string method. It returns a string. It reads a string from the current stream. The string is prefixed with the length encoded as an integer 7 bits at a time. Here we have seen few methods of Binary Reader class. Now pause the video and find the answer. Which method of the Binary Reader class is used to read a string from the current stream? Write the syntax and example of the method. The answer is given here. The syntax is public virtual string read string because it returns a string. Then example is binary reader reader equal to new binary reader. Here we are creating object of binary reader and a specific path is also given so that the file is open for reading. The object of binary reader that is reader is used for reading from the file. Read string method is used on reader object. It will read a string and it will assign it to string variable s. Now we will see binary Reader class. It writes primitive data types in binary to a stream and supports writing strings in a specific encoding. The binary Reader class provides methods that simplify writing primitive data types to a stream. For example, we can use the write method to write a Boolean value to the stream as a one byte value. The class includes write methods that support different data types. When we create a new instance of the binary Reader class, we provide the stream to write to and optionally we specify that type of encoding and whether to leave the stream open after disposing the binary Reader object. There are four constructors of binary Reader class. The first constructor doesn't take any parameters. It initializes a new instance of the binary Reader class that writes to a stream. The second constructor takes one parameter that is the output stream. It initializes a new instance of the binary Reader class based on the specified stream and using utf8 encoding. The third constructor takes two parameters that is output stream and the character encoding to use. It initializes a new instance of the binary Reader class based on the specified stream and character encoding. The fourth constructor takes three parameters that is the output stream, the character encoding to use and the third parameter is leave open. It takes Boolean value either true or false. True to leave the stream open after the binary Reader object is disposed. Otherwise false. Now we'll see methods of binary Reader class. First method is close method. It closes the current binary Reader and the underlying stream. Dispose method releases all resources used by the current instance of the binary Reader class. Flash method clears all buffers for the current Reader and causes any buffered data to be written to the underlying device. Seq method accepts two parameters. Offset is a byte offset relative to origin. Upfed Seq origin indicates the reference point from which the new position is to be obtained. It sets the position within the current stream and returns it. Then we have write method with only parameter that is string. This writes a length prefixed string to the stream in the current encoding of the binary Reader and advances the current position of the stream in accordance with the encoding used and the specific characters are written to the stream. Next method is write. This is a method with Boolean parameter. This writes one byte Boolean value to the current stream with zero representing false and one representing true. Then the next method is write with character parameter writes a unicode character to the current stream and advances the current position of the stream in accordance with the encoding used specific character is written to the stream. Now let's see a program with both binary Reader and binary Reiter classes. In this program we have used classes binary Reader and binary Reiter. So we need to include namespace system.io. This is the main method. Here we have integer variable i which has value 25, double variable d which has value 3.14. Here we have created object of binary Reiter class. First we have specified path, then the mode for creating the file. If this statement raises an exception, if the exception is IO exception that will be handled here. Then we have used write method of binary Reiter class. This write method accepts integer value. So i is integer. We have written i to the particular file. Then we have used write method with double parameter d and this also will be written to the file. If IO exception is raised it will be handled here. Then we have closed binary writer. Now we have created object of binary reader class where we have specified two parameters. The file path and file mode. File mode is open in this case. If the IO exception is raised by the statement it will be handled here. Now here we are reading from the file. We have used method read int 32 to read integer from the particular file and the read value will be stored in i and this will be written or this will be displayed on console. And this read double method reads double value from the stream and it will be stored in d and this is also written to the console. If the IO exception is raised it will be handled in the sketch block. Now the stream is closed through closed method. Let's execute this program. The program has written value 25 and 3.14 to the file and it has again read it from the file. Let's see this file. This is a binary file. So this is not human readable. In this session we have seen binary reader and binary writer classes with example. Thank you.