 Here's a text file named prices.txt that contains prices of items bought on a fairly lengthy shopping trip. We're going to write a program that totals the prices, adds 5% sales tax, and prints the results. Here's the pseudocode. You may want to pause the video and analyze it. To read from a file, you create a scanner object based on a file object. Here's the code for creating a file. As we saw in a preceding video, we'll have a file called price file, and it's a new file, and the path name will be prices.txt. Instead of making a scanner based on system.in, we'll use pricefile and our scanner named input will be a new scanner based on the price file. Unlike keyboard input, where we needed a sentinel value to tell us when we were finished with input, when using files, we can test to see if there's anything left in the scanner's input stream by using the hasnext method. Hasnext returns true if there's data left to read false otherwise. Reading a double is accomplished with input.nextdouble, and closing the scanner is input.close. Let's compile that, and we'll get an error. When you create a scanner from a file, it's possible that the file doesn't exist, and that's a checked exception. The compiler is telling us that this is an exception that we must either catch or throw to a calling method. Let's use try catch. We'll put a try block around the main logic of our code, and then we'll catch file not found exception, and when we catch it, we'll print out an error message. As long as I'm adding a catch block, I'm going to add another one for an input mismatch exception to handle the case where the file has bad data that can't be converted to double. We'll say that there's bad data in the file, and print the exception. Before we can compile again, we have to import those exceptions. We'll import java.util.inputMismatchException and java.io.fileNotFoundException. Let's recompile and run it, and it works fine. If I change the name of the file, from prices.txt to noprices.txt, and then try to run the program again, I get the error that the prices.txt file was not found. Let me rename that to the correct name. And in here, let's put some bad data, like 2-point, and I'll use the letter O instead of the digit 0. Let's run the program again, and we get the bad data in the file. There are a couple of other points I need to cover. First, why did I catch file not found exception, rather than using an if-else with file.exists? Doesn't that go against what I said in a previous video, to use if-else in preference to exceptions? The difference here is that file not found exception is a checked exception. I must deal with it. I don't have the option of using an if-else instead of the exception. Finally, there's a change I would like to make to the code where I read from the file. Rather than reading token by token, it's preferable to read line by line. So instead of using next double, I'll have a string line and read the next line of the file. So I'm reading the file one line at a time, and I'll take that string and convert it to a double. When I use this technique, instead of an input mismatch exception, I will get a number format exception, which I don't need to import. I still have the bad data in the file. So let me recompile and rerun. And this time, not only will I get the name of the exception, I'll also get more useful information as to what caused the exception. Just to make sure that I haven't introduced any further errors, let me go back to the prices.txt file and get rid of the bad data and run the program again. And it still works properly. In summary, what you need to know about reading files from a Java program is to create a file object, make a scanner based on that file object, and then read it one line at a time and process the data on those lines. And remember, when building a scanner with files, you must have a catch for the checked exception, file not found exception.