 Here's a text file named people.txt with a list of names. We're going to write a program that reads in the names, puts them into a last name first form, and writes them out to a new file. This program will need two files, one for input, file in file is a new file based on people.txt as the path, and file out file, which is a new file whose name is switchedpeople.txt. As we've done before, we need to create a scanner for our input file inside a try catch. Our scanner input will be a new scanner based on in file, and we'll catch file not found exception if there's an error. We'll print out the exceptions message. For output, we're going to need a new class called printwriter. We're going to create a printwriter named output, and it will be a new printwriter based on our output file. Creating a printwriter will also throw a file not found exception in case of error. That means we don't have to have another catch block. Here's a loop for reading the input. As long as the input has more data available, we're going to read one line at a time by using the next line method on our input scanner. We'll create a string array called name parts, which will become the result of taking the line and splitting it on blanks. The new name will be the second name on the line, which is name parts sub one, a comma and a blank, and the person's first name, which was the first item on the line. Instead of doing system.out.print line to have output go to the screen, we're going to use the printwriter's write method. We'll have output.write new name. The write method, just like system.out.print, doesn't add a new line when it writes. If you want new lines in the file, and we do, you have to add them yourself. After the loop is finished, we'll close the input file, and, this is ultra important, we have to close the output file as well. Why? Because disks are slow compared to the CPU. Instead of writing data immediately to the disk every time a program has something to write, Java maintains a buffer of data, and fills it up as you write to the printwriter. When the buffer is full, it writes all of that data to the disk. The printwriter's close method makes sure that all the data in the buffer gets written to disk, even if the buffer isn't full yet. Let's compile the program, and let's run it. There's no output to the screen, because all of our output has gone to a file. Let's open switchedpeople.txt, and here are the original names, and the names in lastNameFirstForm. What happens if we add a couple of names to our original list? Let's add Arturo Toscanini, and Neal Sadaka. Save that, and we'll run the program again. This time, the file switchedpeople.txt already exists. The old version of the file is removed, and a completely new file with the same name is created. If I come here, it'll say, the file switchedpeople.txt on the disk is more recent than the current buffer. Do you want to reload it? And when I reload it, there are Arturo and Neal in lastNameFirstFormat. Because opening a printwriter will wipe out an existing file, this is a place where you can use file.exists if you don't want users to overwrite a file that has already been created. One additional note. This program will crash if you add a blank line somewhere in the middle, or if you have someone with only one name, and it'll give incorrect results if you have somebody with more than two names. You might want to try fixing the program to solve these problems. The link in the video description will show you both this program and the solution. In summary, to write to a file, open a printwriter that's based on some file object. Use the write method to write output to the disk file. If you want new lines in your output, you have to put them there yourself. And always be sure to close your printwriter before your program ends.