Welcome to Inheritance and Polymorphism. This is the fourth course in the introduction to computer programming in visual basic specialization. In this course, we're gonnao talk about several things. We're going to talk about file input and output, inheritance, polymorphism, and they were going to finish it out with interfaces. Interfaces are really important in the corporate development world where we have many developers working on the same process. All right, in this first module, we want to look at how we can persist data to files. And the main reason we want to persist data is so that we have the same data across executions of our program. What I mean by that is, it would be really awful if you wrote a program, and then every time you run it, you had to start over with your data, had to re-enter it, right? That's pretty much been the case so far, right? Each time you run the program, you have to fill in the same input values and start over. So here we want to think about how we can save this data between executions to file, all right. So, some objectives here, by the end of this module, I want you to be able to do many things. This includes work with directories and files. I want you to be able to develop computer programs that write text two files. I also want you to be able to develop computer programs that write binary data to files, and the difference being texts just like we see it, right? Like in a note pad kind of application is, we just have texts were writing in pure textual forum versus binary writing bits and bytes. And the reason we want to do binary, it allows us to store more data in the same space. Think about like a zip file, which is an archive that is compressed, right? If you try to look at that file, it looks like garbage because it's in binary format, right? So, once you're able to write that kind of binary data, I also want you to be able to read text back from files, and also read back binary data, okay? So, in less than one, I want to start off by working with file streams, all right. So, the first thing you want to think about is how we create a file stream in visual basic, and the way we do so as we use the new file stream and in the constructor, we're going to send it the path and the mode, we can also add the access and the share parameters. So essentially, file streams are going to allow us to read and write bytes from a file. We also have a stream reader which is going to read text from a stream. And we also have a stream writer which is going to write text to a stream. We're going to have a binary reader which is going to read binary data from the Stream, and a binary writer which is going to write data to the stream, right? So, let's just take a step back. This module is about reading and writing data to a file, but we may want to read and write data to other things. For example, the network. And so, there's this layer of abstraction, so everything we build on this module will be on top of a file stream. But theoretically, you could do it to other types of streams, okay? So, stream reader and stream writer are on top of a file stream for reading and writing text, binary reader and binary writer on top of a file stream for reading and writing binary data. All right, now we want to look at the File Mode Enumeration. And enumeration is essentially a set of values we can use. And so remember, file mode is one of the parameters when we create a file stream. And there are several options here, we have Append, Create, CreateNew, Open, OpenOrCreate and Truncate. So, with append, it's going to open the file if it exists, and it goes to the end of the file, we call that seeking to the end of the file. This is going to require a pen permission on that file, okay? And so, you only want to append if the file doesn't exist, okay? Would create, we're telling the operating system to create a new file. If the file already exists, it's going to be overwritten. Create new, it's going to create a new file. But if the file already exists, it's going to throw an exception. Open, it's going to tell the system to open an existing file. And so essentially, unlike a pen we're not going to set our seat to the end of the file. Open or create is going to open the file if it exists, otherwise a new file will be created. And lastly, truncate. It specifies that the operating system should open an existing file, and when the file is open, it should be truncated so that the sizes zero bytes, okay? We also have a file access enumeration, and you can probably figure out based on these values what they mean. So, read means we want to be able to read data from the file. If you try to write to a file stream that you only have read file access, you'll get a None or an exception. ReadWrite means we want to both be able to read from the file and write to the file, and Write means we only want to be able to write to the file. Now you might say, why do I care? Right? I should just ReadWrite everything. But there's different locking mechanisms in the operating system that we need to think about. So, often you can have multiple readers and one writer, okay? So, if you don't need to write to a file, you should open it with a file access of read so other people can read at the same time. We also have a file share enumeration, which essentially controls how other people can access the file, so we have None, Read, ReadWrite and Write. And this sets that locking mechanism, are we locking for reading? Are we locking for writing? Are we locking for readwriting or no law? All right, so here's some example code. We're going to create a variable called path. I'm setting it to my local temporary directory, and a file called test dot txt. And then I'm going to create a file stream. I'm going to name it Fs, and I pass in that path, and I say I want to create the file, right? So, if the file exists, it's going to be overwritten. So either way, whether it's there or not, I'll get a file. And I want my file access mode to be writing them, only to be writing to the file that I read from this file. All right, that's it for less than one,a little review here. File streams allow you to control how files accessed, the Stream reader and Stream writer are for text data, binary reader of binary radar for binary data. And we'll get more into those as we move forward, all right? See in the next lesson.