Reading from a Text File using File and Scanner
We can read input from a variety of sources. In this lesson, we will explore reading data from a text file. This is great for when you have a lot of data that needs to be entered and hand keying the data would be time-consuming or introduce errors. One excellent beginner use for reading from a text file, is if you want to repeatedly test your program with the same input values. Recording these values in a text file will allow you to test your code without having to re-enter the data through the console each time.
We will also explore other ways to read from a file, but in this lesson we will use File and Scanner, as that is required for the AP Computer Science A exam.
File Class
We can use the File class to represent a file.
Constructor
We can create a File object using a String that contains the file path. If the file is in the same project folder as your .java file, you can use the name of the file as input.
If the file pile has backslash characters (\), you will need to use two, since this is an escape character.
Scanner Class
You can use the Scanner class to read from a file.
Constructor
The Scanner constructor can be used to create a Scanner object with a File object as the parameter.
Wrong File Path
What is the file gets moved? Or you have typed the file name or path incorrectly? Java will throw and excpetion when you try to create a Scanner object with a file path that doesn't exist. These cases, you will need to deal with the exceptions. There are a couple of different ways to deal this.
The AP Computer Science A course recommends that you add throws IOException to the header of the method that is using the file. For example:
Another way that you can handle dealing with exceptions is by using a try..catch statement.
Scanner Methods
int nextInt()returns the nextintread from the file or input source if available. If the nextintdoes not exist or is out of range, it will result in anInputMismatchExceptiondouble nextDouble()returns the nextdoubleread from the file or input source if available. If the nextdoubledoes not exist, it will result in anInputMismatchExceptionboolean nextBoolean()returns the nextbooleanread from the file or input source. If the nextbooleandoes not exist, it will result in anInputMismatchExceptionString next()return the nextStringread from the file or input source.boolean hasNext()returnstrueif there is a next item to read in the file or input source; returnsfalseotherwise.void close()closes thisScannerobject.
Your Turn
Try the following in a desktop or online IDE.
Consider a grades.txt has the following values for grades:
If you wanted to write a program to compute the average of these grades, you could store these grades in grades.txt and repeated run the program until the average yielded the correct result.
Another Example
Try the following in a desktop or online IDE.
Consider an input file that contains the direction and number of degrees a robot should turn. The direction can be L for left and R for right. And the number of degrees is between 0 and 360.
Consider the moves.txt files has the following Robot moves to try:
In this case, we have a mix of letters and numbers. You can separate these into direction and degrees by using a combination of substring and Integer.parseInt().
A program to turn the robot might look like the following:
Resources
Please check back from more resources.