E ExamMaster

Object-Oriented Programming through Java · Packages, Exceptions and I/O

Java I/O and Files

In Object-Oriented Programming through Java because the desk reads typed input and booking files — streams, Scanner, and closing what you opened.

A stream is a sequence of bytes or characters flowing into or out of the program. Scanner breaks typed text into tokens. Files are streams with a path. Opening a file is a promise to close it — try-with-resources writes that promise so an exception cannot skip the close.

  • Object-Oriented Programming through Java
  • Easy level
  • 5 concepts

1A stream is bytes or characters in a line

Java I/O is built on streams: a source or sink you read or write in order. You do not load a whole disk file as one string unless you choose to.

Byte streams (InputStream, OutputStream) move raw bytes. Character streams (Reader, Writer) move text with a charset. Pick characters when the file is a booking list; pick bytes when it is a picture.

No diagram — the idea is carried by the prose or the Java listing.

Which stream family
FamilyUse when
InputStream / OutputStreambytes, images, jars
Reader / Writertext bookings, UTF-8 stubs

2Scanner cuts text into tokens

Scanner reads System.in or a file and hands you the next token: next() for a word, nextInt() for an int, nextLine() for the rest of the line.

It is the typed-input tool from Unit I, now seen as an I/O class. Close a Scanner that owns a file. Do not close one that wraps System.in if the rest of the program still needs the keyboard.

Figure. Scanner cuts "A12 200" on whitespace. next() is the seat word A12; nextInt() is the price 200.

Tokens from the keyboard

import java.util.Scanner;
Scanner sc = new Scanner(System.in);
String seat = sc.next();
int price = sc.nextInt();

Two tokens from a booking line

The line is "A12 200". Scanner.next() then nextInt(). What are seat and price?

  • next()A12
  • nextInt()200

Pro tip. The space is the cut. next() stops at it; nextInt() reads the integer that follows.

Coding lab. Tokenise a booking line runs in the app, with checks on your output.

3A file is a stream with a path

To read bookings.txt you open a character stream on that path and walk lines. Files.newBufferedReader (Path) is the modern form; FileReader is the older one.

The path is not the file's contents. If the path is wrong, you get an IOException — a checked exception the last lesson named.

Figure. Path.of("bookings.txt") names the file; Files.readAllLines walks its lines. A wrong path throws IOException, the checked type the exceptions lesson named.

How it works

  1. A file is a stream with a pathA path names the file. Reading can throw a checked IOException.

Read every booking line

import java.nio.file.Files;
import java.nio.file.Path;
for (String line : Files.readAllLines(
        Path.of("bookings.txt"))) {
    System.out.println(line);
}

4Writing a file is a stream the other way

Files.writeString or a BufferedWriter sends text to a path. You choose whether to replace the file or append.

A write that succeeds on disk is still your data — check the path you passed. Writing "A12 200" is not the same as writing a Ticket object; you choose the text format.

Figure. Files.writeString sends the characters A12 200 to the path. That is not the same as writing a Ticket object; you choose the text format.

How it works

  1. Writing a file is a stream the other wayWriting stores the characters you send. It does not store a Java object unless you choose a format.

Replace bookings.txt with one line

import java.nio.file.Files;
import java.nio.file.Path;
Files.writeString(Path.of("bookings.txt"),
        "A12 200\n");

5try-with-resources closes the stream

try (BufferedReader br = Files.newBufferedReader(path)) opens the reader and guarantees close, even if the body throws.

That is why it exists: a catch or a return used to skip br.close(). The resource must implement AutoCloseable. Scanner and the file streams do.

Figure. try (var br = Files.newBufferedReader(path)) opens the reader and closes it on the way out, even if readLine throws. BufferedReader implements AutoCloseable.

How it works

  1. try-with-resources closes the streamOpen the stream in try (...). Close is then not your finally draft.

br.close runs on the way out

try (var br = Files.newBufferedReader(
        Path.of("bookings.txt"))) {
    String line = br.readLine();
    System.out.println(line);
}

Notes

  • In Object-Oriented Programming through Java because the desk reads typed input and booking files — streams, Scanner, and closing what you opened.
  • InputStream/OutputStream are bytes. Reader/Writer are characters. Scanner tokenises System.in or a file.
  • try (Scanner sc = ...) closes sc for you. A forgotten close leaks the file handle.

Exam traps & shortcuts

  • Scanner.nextInt() leaves the newline behind. A following nextLine() can read an empty line if you do not know that.
  • A path that works on your laptop may not exist on the machine that runs the jar. Prefer a path you control, not a hard-coded home folder.

Recap

Streams move bytes or characters. Scanner tokenises. Files are paths plus streams. try-with-resources closes them.

bytes vs characters
Images are bytes. Booking lists are characters.
Scanner
next, nextInt, nextLine. Watch the leftover newline.
close
try (resource) is the form that cannot skip close.

Practise Java I/O and Files

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.