---
title: "File — AP Comp Sci A Definition & Exam Guide"
description: "A file is persistent data storage your Java program can read at runtime. Learn how File + Scanner work in AP CSA Topic 4.6 and how the exam tests file reading."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/file"
type: "key-term"
subject: "AP Computer Science A"
unit: "Unit 4"
---

# File — AP Comp Sci A Definition & Exam Guide

## Definition

In AP Computer Science A, a file is storage for data that persists when a program is not running and can be retrieved during program execution, typically by creating a File object with the file's name and reading it with a Scanner (EK 4.6.A.1, Topic 4.6).

## What It Is

A file is data that lives outside your program. [Variables](/ap-comp-sci-a/unit-1/expressions-and-assignment-statements/study-guide/01dr6uUPDAn3SjtK2Psr "fv-autolink") and ArrayLists vanish the moment your program ends, but a file sits on disk and keeps its data whether your code is running or not. When your program needs that data, it can open the file and read it back in during execution (EK 4.6.A.1).

In Java, you connect a file to your program with two classes working together. First you create a `File` [object](/ap-comp-sci-a/key-terms/object "fv-autolink"), passing the file's name as a String to the [constructor](/ap-comp-sci-a/unit-1/creating-and-storing-objects/study-guide/rUOTKl6Ih5noXJ0GtxJF "fv-autolink"), like `File f = new File("grades.txt")` (EK 4.6.A.3). Then you hand that File object to a `Scanner`, which actually pulls the data out token by token or line by line (EK 4.6.A.2). One catch you can't skip is that the file might not exist. Java forces you to say what happens in that case, which is why file-reading methods carry `throws IOException` in their signatures (EK 4.6.A.4). Think of the File object as the address of the data and the Scanner as the delivery truck that brings it into your program.

## Why It Matters

Files live in Topic 4.6 (Using Text Files) inside [Unit 4](/ap-comp-sci-a/unit-4 "fv-autolink"): Data Collections, under learning objective 4.6.A, which asks you to develop code that reads data from a text file. The placement is the point. Unit 4 is all about handling many pieces of data at once, and files are where real-world data actually comes from. A program that computes class averages doesn't hard-code 30 test scores; it reads them from something like grades.txt. So file reading is the bridge between the outside world and the data structures you process with loops, ArrayLists, and 2D arrays. If you can read a file into a collection and then [traverse](/ap-comp-sci-a/key-terms/traverse "fv-autolink") that collection, you've connected the two big skills of Unit 4.

## Connections

### [Scanner class (Unit 4)](/ap-comp-sci-a/key-terms/scanner-class)

Scanner is the file's reading partner. A File object only points at the data; Scanner [methods](/ap-comp-sci-a/unit-3/abstraction-and-program-design/study-guide/o9VgVeIpKRYZ7N7rXfUz "fv-autolink") like next(), nextInt(), and hasNext() are what actually pull values into your program. Almost every file question is really a File-plus-Scanner question.

### ArrayList (Unit 4)

Files and ArrayLists solve opposite halves of the same problem. The file holds data when your program is off, and the ArrayList holds it while your program runs. The classic pattern is to [loop](/ap-comp-sci-a/key-terms/loop "fv-autolink") with hasNext(), read each value, and add it to an ArrayList for processing.

### While loops and traversal (Unit 1 / Unit 4)

You usually don't know how many lines a file has, so you can't use a fixed-count [for loop](/ap-comp-sci-a/unit-2/for-loops/study-guide/DJuLxKz6SiSAX2cEVmCt "fv-autolink"). File reading is the textbook use case for a while loop with a sentinel-style condition, here written as while(scan.hasNext()).

### Exceptions and IOException (Unit 4)

Opening a file is the first place the CED makes you confront failure. The file might not exist, so Java requires you to declare what happens, which is why getHonorStudents-style methods are written with throws IOException in the header.

## On the AP Exam

File reading is tested through code analysis and code completion. A typical question gives you a text file's contents, like "Alice 95" on each line of grades.txt or "Apples 50 1.25" in inventory.txt, then asks which code segment correctly reads it or what the code prints. To get these right you have to match the Scanner calls to the file's format. If each line has a name then an int then a double, the read order has to be next(), nextInt(), nextDouble(). You also need to recognize the full setup pattern, which is constructing a File with the filename String, wrapping it in a Scanner, looping with hasNext(), and declaring throws IOException on the method. No released FRQ has required file-reading code, since Topic 4.6 is a newer addition to the course, but the skill it builds (looping over incoming data and storing it in a collection) is exactly what Unit 4 FRQ-style methods like getHonorStudents practice.

## file vs File vs. Scanner

The File object does not read anything. It just represents the file's name and location on disk. Scanner is the class with the reading methods (next, nextInt, nextLine, hasNext). You need both, and in this order: create the File, then pass it to the Scanner constructor. Writing something like file.nextInt() is a classic wrong-answer trap because File has no reading methods.

## Key Takeaways

- A file is storage for data that persists when the program is not running, and that data can be retrieved while the program executes (EK 4.6.A.1).
- You open a file by creating a File object with the filename String as the constructor argument, like new File("grades.txt").
- The File object only points to the data; you pass it to a Scanner, and the Scanner's methods actually read the values.
- Use while(scan.hasNext()) to read a file because you usually don't know in advance how many values it contains.
- Java requires you to handle the case where the file doesn't exist, which is why file-reading methods declare throws IOException.
- When reading formatted lines like "Alice 95", the order of your Scanner calls (next() then nextInt()) must match the order of data in the file.

## FAQs

### What is a file in AP Computer Science A?

A file is storage for data that persists when a program is not running and can be read back in during program execution. In AP CSA you work with text files in Topic 4.6, opening them with the File class and reading them with Scanner.

### Is reading files actually on the AP CSA exam?

Yes. Topic 4.6 (Using Text Files) is in Unit 4 of the CED, and learning objective 4.6.A says you should develop code to read data from a text file. Expect multiple-choice questions that show a file's contents and ask which code segment reads it correctly.

### What's the difference between the File class and the Scanner class?

File represents the file itself (you create it with the filename, like new File("data.txt")), while Scanner does the actual reading with methods like nextInt() and hasNext(). File has no reading methods, so you always wrap the File in a Scanner.

### Why do file-reading methods need throws IOException?

Because the file might not exist when your program tries to open it. The CED (EK 4.6.A.4) requires you to indicate what to do in that case, and on the exam that shows up as throws IOException in the method signature.

### Do I need to know how to write to files for AP CSA?

No. The CED only covers reading from text files (LO 4.6.A says develop code to read data from a text file). Writing to files, binary files, and other I/O libraries are beyond the scope of the exam.

## Related Study Guides

- [4.6 Using Text Files](/ap-comp-sci-a/unit-4/using-text-files/study-guide/JhiQXb5r4daUivzP1Xkm)

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/file#resource","name":"File — AP Comp Sci A Definition & Exam Guide","url":"https://fiveable.me/ap-comp-sci-a/key-terms/file","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/file#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"dateModified":"2026-06-11T05:27:19.007Z","isPartOf":{"@type":"Collection","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"},"publisher":{"@type":"Organization","name":"Fiveable","url":"https://fiveable.me"}},{"@type":"DefinedTerm","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/file#term","name":"file","description":"In AP Computer Science A, a file is storage for data that persists when a program is not running and can be retrieved during program execution, typically by creating a File object with the file's name and reading it with a Scanner (EK 4.6.A.1, Topic 4.6).","url":"https://fiveable.me/ap-comp-sci-a/key-terms/file","inDefinedTermSet":{"@type":"DefinedTermSet","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"}},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is a file in AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"A file is storage for data that persists when a program is not running and can be read back in during program execution. In AP CSA you work with text files in Topic 4.6, opening them with the File class and reading them with Scanner."}},{"@type":"Question","name":"Is reading files actually on the AP CSA exam?","acceptedAnswer":{"@type":"Answer","text":"Yes. Topic 4.6 (Using Text Files) is in Unit 4 of the CED, and learning objective 4.6.A says you should develop code to read data from a text file. Expect multiple-choice questions that show a file's contents and ask which code segment reads it correctly."}},{"@type":"Question","name":"What's the difference between the File class and the Scanner class?","acceptedAnswer":{"@type":"Answer","text":"File represents the file itself (you create it with the filename, like new File(\"data.txt\")), while Scanner does the actual reading with methods like nextInt() and hasNext(). File has no reading methods, so you always wrap the File in a Scanner."}},{"@type":"Question","name":"Why do file-reading methods need throws IOException?","acceptedAnswer":{"@type":"Answer","text":"Because the file might not exist when your program tries to open it. The CED (EK 4.6.A.4) requires you to indicate what to do in that case, and on the exam that shows up as throws IOException in the method signature."}},{"@type":"Question","name":"Do I need to know how to write to files for AP CSA?","acceptedAnswer":{"@type":"Answer","text":"No. The CED only covers reading from text files (LO 4.6.A says develop code to read data from a text file). Writing to files, binary files, and other I/O libraries are beyond the scope of the exam."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"AP Computer Science A","item":"https://fiveable.me/ap-comp-sci-a"},{"@type":"ListItem","position":2,"name":"Key Terms","item":"https://fiveable.me/ap-comp-sci-a/key-terms"},{"@type":"ListItem","position":3,"name":"Unit 4","item":"https://fiveable.me/ap-comp-sci-a/unit-4"},{"@type":"ListItem","position":4,"name":"file"}]}]}
```
