6m left·0%
Reading Time: 6 min
Last Updated: March 17, 2026
Main Ideas: 5
Reading Time: 6 min
Last Updated: March 17, 2026
Main Ideas: 5

Topic 4.6 Notes – Using Text Files

Verified for 2027 AP® Computer Science A Exam
Read aloud
Text files allow your Java programs to read data that exists outside the program itself. Unlike variables and arrays, which disappear when the program ends, files store data permanently on disk. In AP Computer Science A, you focus specifically on reading from text files using File and Scanner.

1. What Text Files Are and Why They Matter

A file is persistent storage. That means the data still exists after your program finishes running.

  • Variables / arrays → live in memory, disappear when program ends
  • Files → stored on disk, still there tomorrow

A text file contains readable characters. You can open it in Notepad or any text editor.

Your variables and arrays exist in memory (RAM). When the program stops, that memory is cleared. Files are stored on the disk, which is why they remain after the program ends.

Programs read files to:

  • Process large datasets
  • Load saved information
  • Analyze stored values

For AP, you are not writing files. You only need to know how to read them.

2. Setting Up File Input Correctly

There’s a specific pattern you’ll see again and again.

Required Imports

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
  • File and IOException come from java.io
  • Scanner comes from java.util

If you forget these, it won’t compile.

Create the File Object

File dataFile = new File("data.txt");

This does not read the file yet.
It just creates an object representing the file’s path.

Handle IOException

Java forces you to acknowledge that file access might fail.

The AP-style solution:

public static void main(String[] args) throws IOException

If the file doesn’t exist, the program stops. That’s fine for AP.

Connect Scanner to the File

Scanner input = new Scanner(dataFile);

Now you can use familiar Scanner methods.

Always Close the File

input.close();

Closing the Scanner closes the file. Expect to lose points if you forget this on an FRQ.

3. Scanner Methods You Must Know

These work exactly like Scanner with keyboard input.

Token-Based Reading

  • next() → next word
  • nextInt() → next integer
  • nextDouble() → next double
  • nextBoolean() → next boolean

If the next token isn’t the expected type, Java throws an InputMismatchException.

Line-Based Reading

  • nextLine() → returns the entire next line

Be aware:

  • nextLine() behaves differently with whitespace.
  • Mixing nextLine() with nextInt() can cause empty strings.

The AP exam does not require complex mixing situations, but you should recognize why it might happen.

Checking Before Reading

These prevent crashes:

  • hasNext()
  • hasNextInt()
  • hasNextDouble()

Example loop pattern:

while (input.hasNextInt()) {
    int value = input.nextInt();
}

This runs until the file runs out of integers.

That while (input.hasNext___()) pattern is the key idea.

4. Processing File Data with Algorithms

Once you read the data, it becomes a normal loop problem.

Counting Pattern

int count = 0;

while (input.hasNext()) {
    input.next();
    count++;
}

Sum and Average Pattern

int sum = 0;
int count = 0;

while (input.hasNextInt()) {
    int num = input.nextInt();
    sum += num;
    count++;
}

double average = (double) sum / count;

This shows up constantly on tests.

Storing in an ArrayList

ArrayList<Integer> numbers = new ArrayList<>();

while (input.hasNextInt()) {
    numbers.add(input.nextInt());
}

Very common FRQ move:

  1. Read values
  2. Store in ArrayList
  3. Process later

Reading Structured Data

If a file looks like:

Liam 84
Maya 91

You’d read it like:

while (input.hasNext()) {
    String name = input.next();
    int score = input.nextInt();
}

You must trust the file format matches your reading pattern.

Using split

If you read a whole line:

String line = input.nextLine();
String[] parts = line.split(" ");

split(" ") breaks the line at spaces and returns a String[].

You are not expected to use complex regular expressions.

5. Common Exam Mistakes

  • Forgetting throws IOException
  • Forgetting input.close()
  • Using hasNext() when reading only integers
  • Calling nextInt() without checking hasNextInt()
  • Mixing nextLine() and nextInt() incorrectly

A subtle one: using while (input.hasNext()) but calling nextInt() inside. If the next token isn’t an int, it crashes.

On the AP exam, they often give you the file setup and expect you to focus on the loop logic. Treat file input like array traversal. Same patterns. Same algorithm thinking.

Key Takeaways

A file is persistent storage; variables are not.
File represents the path; Scanner actually reads the data.
Always add throws IOException when opening a file.
The core loop pattern is while (input.hasNext___()).
Use the correct hasNext method that matches what you’re reading.
Always call close() when finished reading.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining