In this lecture, we'll learn how to do file IO with text files. Here's the typical file IO pattern. First, we open or create a file, then we read from it or write to it, and then we close the file when we're done. And we include all of those actions with an appropriate try, catch, and finally, blocks to make sure that doing this file IO doesn't either crash our program or lead to misbehavior in our program. Given that information, you should do an in-video quiz. And now, we'll go to MonoDevelop and see how all that works. Okay, here's our example code. And you'll notice that I've added a using directive for the system.IO namespace at the top, because when we're doing file IO, we regularly need access to the classes in that namespace. All I'm going to do is I'm going to write some stuff to a text file, and then I'm going to read this stuff back from that text file. Here's the writing part. First, I'm going to use a StreamWriter for my output. And let's look at the documentation for StreamWriter. So, the StreamWriter class implements a TextWriter class for writing characters to a stream. And if we click on TextWriter, we see that this is a writer that can write a sequential series of characters, and the important methods that we get are Write and WriteLine. So, we can use Write and WriteLine just as we have on the console class. Back in the StreamWriter class, we need to know how to construct an object of this class. So, we're going to use this first constructor where we pass in a stream. In the code, I have a try block here because this code could throw an exception, and I want to catch an exception if it throws it. So, the first thing I'm going to do is call a static method in the file class that will create a text file, and here's the name of that text file I want to create. And, although, this isn't our standard calling a constructor syntax where we would say new StreamWriter with the stream in here, this is, sort of a syntactic shortcut, and this is creating a new StreamWriter. Now, it might not be able to. If we're trying to create the text file somewhere on the computer where we're not allowed to create new files, this would throw an exception. So, there are possibilities for this code throwing an exception. The next thing we do is we just write a few lines to that text file, and that's all the stuff that might throw an exception. We have a catch block to catch any exception that might get thrown, and we'll print a message from that exception to the window if we need to. And then we have this, finally block that we talked about previously when we were talking about exception handling, where we always want to close the output file, as long as we created one. So, let's just go ahead and run this code, alt control F5. And we didn't see any output, of course, because all the output went to a file. Now, where is that file? It's in your project folder in the bin debug folder, and it will be called OneStepCloser.text, for this example. So, I'm going to open up that file. And, as you can see, it output the lines that we said we wanted it to output into that file. That's it for file output. There are lots of other details associated with file output, like if you wanted to append to a file that already has some content in it, and so on. We won't worry about that here, this is just the basic text file output. The other thing we, might as well, see how to do is text file input. So, that's the next block of code in this example code. And one of the easiest ways to do text file input is to use the StreamReader class, which works much like the StreamWriter class. This time, we're going to read from a stream. So again, we included a try block, and inside the try block we're going to open a text file called OneStepCloser.text, and the code assumes that that text file is in the same location as the executable is. If it's not, you can give a fully qualified path to the file here, if you need to do so. But I'm just going to read back the file I just created, so I know it's right there with the executable. This next chunk of code reads and echoes the lines until the end of file. So, here's how this works: input.ReadLine where input is a StreamReader, just reads a line of code from the file, so it reads the sequence of characters up to the carriage return, and puts it into this variable called line. Now, when it gets to the end of the file and we've tried to read in the line, it doesn't return a string, it returns null. So, I can safely check if we're at the end of the file here, by checking line not equal null. So, while line isn't equal null, I'm going to WriteLine that line, this time to the console, not to an output file but to the console, and then I'm going to read the next line. So, I just keep doing this loop until a ReadLine, either this one, if it really didn't have anything in the file, or this one, if I've read one or more lines, until one of those two returns null, at which point I'm done with this while loop. I catch exceptions as always, and I have a finally block that always closes the input file, as long as the input file was able to be opened. So, this is why we covered exception handling before we covered file IO, because we want to enclose lots of our file operations into try blocks and finally blocks. And we include catch blocks as well, just in case something goes wrong. So now, if I control F5, that is the echoing of that file that we wrote in the previous block of code, and now we have read each line and echoed them to the command prompt window here, as we worked our way through the file. Before we end this lecture, you should do another in-video quiz about file IO. To recap, file IO is a lot like using the standard input and output streams, except that, we use exception handlers to make sure that we don't crash our program or lead to erroneous behavior over our program through the exceptions. So, we handle them instead. But doing the actual reading and writing is very similar to what we've done.