Java Reading a File
Prepare a plain text file named note.txt in the same folder as your program. Put one short line inside it: oak. Open Reader.java, build a File for note.txt, wrap it in a Scanner, print nextLine(), then close the scanner. Compile with javac Reader.java and run java Reader. The screen should show oak. A second demo opens missing.txt instead and stops with FileNotFoundException. Leave writing a file with Java for the next page — create note.txt by hand in an editor for this lesson.
Read a line that is already on disk
Earlier pages stopped with exceptions you caught or threw. This page reaches letters that live in a file after the program ends. Open a teaching file named Reader.java. Keep the public class name Reader so the file name matches. Delete leftover Signal throw demos, leftover Guard catch blocks, leftover Alarm array stops, and any Scanner that reads from System.in with keyboard nextLine. The program must not wait for typing. Reading a file still uses Scanner, but the constructor takes a File, not the keyboard.
You need three import lines near the top:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
Mark main with throws FileNotFoundException so a missing file can surface as that checked exception. Inside main, name the disk file:
File note = new File("note.txt");
Build a scanner on that file:
Scanner scan = new Scanner(note);
Print the first line:
System.out.println(scan.nextLine());
Then finish the read:
scan.close();
Before you compile, create note.txt beside Reader.java and type exactly oak on the first line. Save the text file. The Java program does not create that text file on this page.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Reader {
public static void main(String[] args) throws FileNotFoundException {
File note = new File("note.txt");
Scanner scan = new Scanner(note);
System.out.println(scan.nextLine());
scan.close();
}
}
From the folder that holds both Reader.java and note.txt, run javac Reader.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Reader — not java Reader.java. Leave the .java ending off the run command. If javac cannot find File or Scanner, the import lines are missing or mistyped — put them back before you chase other errors.
The screen shows oak. The scanner read the first line from the disk file and printed it. The program should not pause for keyboard input. If the terminal waits for you to type, you still connected Scanner to System.in instead of to File note. Fix the constructor so it receives the File. If you see empty or a stack from IllegalArgumentException, a Signal throw leftover is still running — switch to java Reader with the file-read teaching file. If you see 2 or blocked, an older Alarm or Guard demo is still the class you are launching.
Walk the happy path once. Java starts at Reader.main. The File object holds the name note.txt. The Scanner opens that name. nextLine pulls the first line. println shows oak. close ends the scanner. Nothing else should print. If the folder has no note.txt, you will hit the missing-file story early — create the text file first, then rerun.
nextLine here means the next line in the file, not a prompt for the user. That reuse of the method name is easy to mix up after the input page. Keep the story straight: same method name, different source. The letters oak live in the text file; the Java class only reads them.
📊 File and Scanner roles
| Piece | What it does on this page |
|---|---|
| File | holds the disk name such as note.txt |
| Scanner | reads lines from that File with nextLine |
| close | finishes the Scanner read so the file handle is released |
A missing name throws FileNotFoundException
A second program keeps the same shape but points at a name that is not on the machine: missing.txt. Do not create that file. The run should stop with FileNotFoundException.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Reader {
public static void main(String[] args) throws FileNotFoundException {
File note = new File("missing.txt");
Scanner scan = new Scanner(note);
System.out.println(scan.nextLine());
scan.close();
}
}
Compile with javac Reader.java, then run java Reader. This time the program does not print oak. The terminal reports FileNotFoundException. The throws clause on main lets that checked exception leave main without a catch on this page. You already practiced catch earlier; here the teaching point is recognizing that a missing file name stops the read. Spelling of the exception type matters when you talk about it: FileNotFoundException. Saying only “error” is too vague for this lesson.
If you accidentally create missing.txt, delete it or rename the string back to a name that is absent. Prefer the successful note.txt demo when you want a quiet screen with oak. Prefer the missing-name demo only to see FileNotFoundException. Switch files deliberately so the happy read and the missing-name stop are not mixed in one confused main while you study.
Do not invent a Java write on this page to create note.txt. Creating the text file in an editor is enough for reading practice. The next page is writing a file with Java so a program can store letters itself. Paths and more file tools come later; this page keeps one clear read and one clear missing-name stop.
Compared with throw on Signal
On the throwing page, java Signal caused a stop with IllegalArgumentException when cups were empty. On this page, the stop for a missing file is FileNotFoundException, and the happy path prints a line from disk. Keep Signal files only as history if they still sit nearby; run java Reader for the file demos so the screen matches this lesson.
Wrappers, arrays, and catch history stay history. Today the new tokens are File, Scanner on a file, nextLine from disk, and FileNotFoundException.
What this leaves for writing files
This page only reads a file that already exists, or stops when the name is missing. The next page shows writing letters into a file with Java. Stay with reading oak and naming FileNotFoundException here. Do not add a write call as a requirement on these Reader demos yet.
Before writing files
You can read note.txt with Scanner and see oak. You can open missing.txt and see FileNotFoundException. Compile Reader.java and run java Reader for each kept demo in turn. Create note.txt by hand beside the class file. The next page is writing a file, where a program stores text itself.
Keep the successful read for a quiet screen. Keep the missing-name demo to see the stop. Keep Java writing off this page. Those three habits are the checkpoint before you move on. When oak appears after java Reader with note.txt present, reading a file did its job for this lesson and you are ready for the next topic about writing files.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top