The AP CS A Exam uses the File
and Scanner
classes to read data from text files.
The examples below assume familiarity with the specific methods of the Scanner
class used on the AP CS A Exam. See Using the Scanner class for explanations and examples.
The examples below are adapted from the 2021 ClubMembers Free Response (#3). (The original question does not feature reading from a file.)
Text file for examples
members.txt contains the data to be read.
2019 false SMITH, JANE
2018 true FOX, STEVE
2017 false XIN, MICHAEL
2020 true GARCIA, MARIA
Each line has a 4 digit int
representing the graduation year, followed by a boolean
representing whether the member is in good standing, followed by a String
representing the member’s last name (and a comma), followed by a String
representing the member’s first name.
The data elements on each line are each separated by a space (" "
).
Example 1
public static void main(String[] args) throws IOException
{
printFromFile("members.txt");
}
public static void printFromFile(String filePath) throws IOException
{
Scanner fromFile = new Scanner(new File(filePath));
while(fromFile.hasNext())
{
int year = fromFile.nextInt();
boolean good = fromFile.nextBoolean();
String last = fromFile.next();
String first = fromFile.next();
System.out.println("[" + last + " " + first + " " + year + " " + good + "]");
}
fromFile.close();
}
The code prints:
[SMITH, JANE 2019 false]
[FOX, STEVE 2018 true]
[XIN, MICHAEL 2017 false]
[GARCIA, MARIA 2020 true]
The usage of Scanner methods (hasNext
, nextInt
, nextBoolean
, and next
) is the same here as in Using the Scanner class Example 2. The explanation below focuses on the use of a File
object.
The printFromFile
method accepts parameter filePath
, a path to a text file. If the text file is in the same directory (folder) as the Java file, the path might just be the file name (ex: "messages.txt"
). If the text file is in a different directory, the path might include additional directories (ex: "data/members.txt"
);
The statement:
Scanner fromFile = new Scanner(new File(filePath));
constructs two objects. First, a File
object is constructed. The File
constructor accepts the file path (a String
). Next, a Scanner
object is constructed. The Scanner
constructor accepts a File
. (Both the File
and Scanner
classes have additional constructors that are not featured on the AP CS A Exam.)
It is possible to store a reference to the File
object on a different line; however, there is often little reason to do so since the code does not need access to the File
except through the Scanner
.
If the file is not found, such as if the path is incorrect, the Scanner
constructor throws the checked exception FileNotFoundException
(a subclass of IOException
).
A method that includes a statement that potentially throws a checked exception must either catch the exception or declare that the exception might be thrown. The header for printFromFile
includes the clause throws IOException
. The main
method calls printFromFile
, so it also includes the clause throws IOException
. (Catching the exception is not covered on the AP CS A Exam.)
The call fromFile.close();
releases the file for use by other programs.
Example 2: nextLine
and split
public static void main(String[] args) throws IOException
{
printFromFile("members.txt");
}
public static void printFromFile(String filePath) throws IOException
{
Scanner fromFile = new Scanner(new File(filePath));
while(fromFile.hasNext())
{
String[] parts = fromFile.nextLine().split(" ");
System.out.println("[" + parts[2] + " " + parts[3] + " " + parts[0] + " " + parts[1] + "]");
}
fromFile.close();
}
The code produces the same output as Example 1; however, the approach is different.
The exmaple uses the Scanner
method nextLine
to read an entire line from the file. The line is immediately broken into individual String
objects using the String
method split
method. split
returns an array of String
objects. See split on the AP CS A Exam for details on the split
method.
Although the output is the same as in Example 1, there is an important distinction between the approaches. The approach in Example 1 obtains the data from the file as different types (int
for year
, boolean
for good
). Example 2 obtains each piece of data as a String
.
Help & comments
Get help from AP CS Tutor Brandon Horn