The File class is used to read the contents of a file.
- It only describes a path; use Scanner/PrintWriter for contents.
The File class, writing text with PrintWriter, try-with-resources that closes files automatically, and reading text with Scanner.
Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.
File represents a file or directory path; it does NOT read or write contents.
The File class is used to read the contents of a file.
Example code.
PrintWriter output = new PrintWriter(file); print/println/printf; close() at the end.
Which class writes text data to a file?
try-with-resources is used to:
Scanner input = new Scanner(new File("scores.txt")); while (input.hasNext()) …
Trap new Scanner("scores.txt") reads the STRING, not the file. Wrap it in new File(...).
Example code.
Example output.