AP® Computer Science A glossary

File (class)

Also called: File

Course: AP® Computer Science A Shows up in: Unit 4: Data Collections

The java.io class whose constructor File(String str) connects a program to the file with that pathname so a Scanner can read it.

Appears in the course and exam description (CED): 4.6.A.24.6.A.34.6.A.44.6.A.5

A File object does not read anything by itself; pass it to new Scanner(f). The method that uses it must say what happens if the file cannot be opened, for example with throws IOException.

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Open
{
    public static void main(String[] args) throws IOException
    {
        File f = new File("scores.txt");
        Scanner input = new Scanner(f);
        System.out.println(input.nextInt());
        input.close();
    }
}
Learn it properly, with a checkpoint 4.6 Using Text Files

Related terms

Was this page helpful?

No names are ever shown, and every public comment is checked by us first. Please do not include personal details. To get a reply, email [email protected].

↑ Top

Enlarged view of the selected figure