 Hello everyone and welcome to the session on files and streams in C-sharp.net. At the end of the session, student will be able to implement C-sharp.net programs to perform read or write operations to files using file stream class. File and stream IO refers to the transfer of data either to or from a storage medium. The .NET framework has a system.io namespace which has various classes, structures, enumerations and delegates that are used to perform read or write operations to files and data streams and types that provide basic file and directory support. A file is an ordered and named collection of bytes that has persistent storage. When we work with files, we work with directory paths, disk storage and file and directory names. In contrast, a stream is a sequence of bytes that we can use to read from and write to a backing store, which can be one of several storage mediums, for example disks or memory. Just as there are several backing stores other than disks, there are several kinds of streams other than file stream such as network, memory and pipe streams. In this session, we will only concentrate on file stream class. The physical file may be text file or binary file. Text file is human readable because everything is stored in terms of text. In binary file, everything is written in terms of 0s and 1s. Therefore, binary file is not human readable. The stream classes such as stream reader, stream writer and file stream inherit from abstract classes, text reader, text writer and stream respectively. Abstract class stream provides functionality for representing streams as bytes. Classes file stream, memory stream and buffer stream inherit from class stream. Class file stream can be used to write data to and read data from files. The abstract base class stream supports reading and writing bytes. All classes that represent streams inherit from the stream class. Streams involve three fundamental operations. Those are reading, writing and seeking. Reading that is transferring data from a stream into a data structure such as an array of bytes. Writing that is transferring data to a stream from a data source. Seeking is querying and modifying the current position within a stream. File stream is commonly used stream class for reading and writing to a file. Constructor initializes a new instance of the file stream class with a specified path, creation mode, read write permission and sharing permission etc. Have a look at the overloaded versions of the file stream class constructors shown here. Let's see in detail file stream constructor with parameters path, file mode, file access and file share. The constructor initializes a new instance of the file stream class with a specified path, creation mode, read or write permission and sharing permission. First parameter path is a relative or absolute path of the file. When parameter is mode, it specifies how the operating system should open a file. File mode is enum. The enumeration values determine how to open or create the file. It has fields called create, create new, open, open or create, append, truncate. Let's see each one in detail. First is create. It specifies that the operating system should create a new file. If the file already exists, it will be overwritten. The next field is create new. It specifies that the operating system should create a new file. If the file already exists, an IO exception is thrown. Next field is open. It specifies that the operating system should open an existing file. A file not found exception is thrown if the file does not exist. Open or create specifies that the operating system should open a file if it exists, otherwise a new file should be created. Next field is append. It opens the file if it exists and seeks to the end of the file or creates a new file. And last field is truncate. It specifies that the operating system should open an existing file. When the file is opened, it should be truncated so that it sizes 0 bytes. The next parameter is file access. It defines constants for read, write, read and write access to a file. Read gives read access to the file. Data can be read from the file. Read write gives read and write access to the file. Data can be written to and read from the file. Write gives write access to the file. Data can be written to the file through this write constant. parameter is file share. It determines how the file will be shared by processes. The possible values are delete, inheritable, none, read, read write, write, etc. Delete allows subsequent deleting of a file. Inheritable makes the file handle inheritable by child processes. Declines sharing of the current file. Any request to open the file by this process or another process will fail until the file is closed. Read allows subsequent opening of the file for reading. But if the flag is not specified, any request to open the file for reading by this process or another process will fail until the file is closed. Read write allows subsequent opening of the file for reading or writing. But if this flag is not specified, any request to open the file for reading or writing by this process or another process will fail until the file is closed. Next is write. It allows subsequent opening of the file for writing. But if the flag is not specified, any request to open the file for writing by this process or another process will fail until the file is closed. Now pause the video and find the answer. Choose the file mode field which is used to create a new output file with the condition that file with the same name must not exist. The answer is option A. With this option, operating system should create a new file. If the file already exists, an IO exception is thrown. Now we will see methods of file stream class for reading and writing. The read method with three parameters. Buffer, offset, count. Reads a block of byte from the stream and writes the data in a given buffer. It reads a maximum of count bytes from the current stream and stores them in a buffer beginning at offset position. The current position within the stream is advanced by the number of bytes read. The read method returns zero if the position is at the end of the file. The next read method with one parameter reads a sequence of bytes from the current file stream and advances the position within the file stream by the number of bytes read. Next we will see write method with one parameter. It writes a sequence of bytes from a read only span i.e. buffer to the current file stream and advances the current position within the file stream by the number of bytes written. Write method with three parameters. It writes a block of bytes to the file stream. The buffer contains data to write to the stream. Offset in array from which to begin copying bytes to the stream. Count is the maximum number of bytes to write. Now let's see a program using file stream class. The example uses utf8 encoding object to encode a string of unicode characters and store them in byte array. This is the example of file stream class. We have used file stream class so we need to include system.io namespace. And for using utf8 encoding this class we need to use system.text namespace. The execution will start from main method. Path is a string variable. It stores the path of the mytest.txt file. This is a text file. If the file exists it will be deleted. Then fs is the reference of file stream. Here we have created a new file with this path. The initial value of i is 48. This loop will repeat till i is equal to less than or equal to 57. When i becomes 58 the loop will be terminated. fs is the reference of file stream. This two care method converts this integer value i to a unicode character. So integer 48 is unicode 0. And then it will be converted to string. And both these parameters fs and the converted string will be passed to add text method. This is the add text method. We have reference of file stream class and a string value. Then here we have used utf8 encoding and get bytes method. Get bytes method will convert this string to a byte array. On left hand side we have byte array info. So this value will be converted to byte array. And then through this method write we have written this byte array to the file. And this will be done for all these values from 48 to 57. This loop will be repeated. So values from 0 to 9 are written to the file. Then after writing to the file we have read from the file. So files open read method is used for reading from the file. And the path is specified here. This b is the byte array. And this read is the method of file stream. It will read from the file and it will store the value in byte array b. And then it will be displayed on console. Let's execute this program. The values are the values written to the file are 0 to 9. And those are displayed after reading from the file. Let's see where the file exists. This is mytest.txt. It has the value 0 to 9. In this session we have seen file stream class with example. Thank you.