AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Skip to content

18 I/O, files and time / Checkpoint

Checkpoint — I/O, files and time

28 min · +25 XP · 3 exercises · 15 questions

Exercises0/3
Quiz · 70% to clear15 q

This checkpoint covers byte and character streams with charsets and the decorator stack, buffering and the fast-I/O template, Path arithmetic and the Files API, Java serialization and its knobs, CSV parsing with quotes, and java.time — the types, arithmetic, zones and formatting.

How it works. Fifteen questions and three programs; 70% on the questions and every program accepted clears the module.

Before you start, make sure you can answer:

  • Why read() returns int, and what length() versus getBytes(UTF_8).length count.
  • The order of the decorator stack from a raw InputStream to readLine().
  • Why Scanner is slow, what buffering saves, and why a program with a PrintWriter printed nothing.
  • What resolve does with an absolute argument, and normalize versus toRealPath.
  • Which Files methods return lazy streams that must be closed.
  • What serialVersionUID and transient do, and why untrusted deserialisation is dangerous.
  • Why split(",") is not a CSV parser.
  • LocalDateTime versus ZonedDateTime versus Instant; Period versus Duration; what Jan 31 plusMonths(1) gives.

The programs are a log analyser that reads until end of input and measures the span with java.time, a hex dump of raw bytes, and a CSV-to-fixed-width report using the quote-aware parser.

Common questions

What is the decorator stack from a raw InputStream to readLine()?

A FileInputStream or System.in supplies bytes, an InputStreamReader decodes them into characters with a charset, and a BufferedReader batches the reads and adds readLine(). Files.newBufferedReader(path) builds the same stack, with UTF-8, in one call.

Which Java Files methods return lazy streams that must be closed?

Files.lines, Files.list, Files.walk and Files.find return streams backed by an open file or directory handle, so each one belongs in a try-with-resources block.

Why is split(",") not a CSV parser?

A quoted CSV field may itself contain commas, doubled quotes or line breaks, and split(",") cuts it in the wrong places. A correct parser tracks whether it is inside quotes and treats a doubled quote as an escaped one.

Java I/O Quiz: Streams, Files and java.time Practice Test

Test Java I/O with 15 questions and three programs on streams and charsets, fast input, Path and Files, serialization, CSV parsing and the java.time API.

  • Course: Java study plan
  • Module: I/O, files and time
  • Kind: Checkpoint — cleared at 70%
  • Reading time: 28 min
  • Runtime: OpenJDK 18

Checkpoint — I/O, files and time is the checkpoint that closes the I/O, files and time module: a graded quiz and whole-program exercises, passed at 70%.

Instructions

This checkpoint covers byte and character streams with charsets and the decorator stack, buffering and the fast-I/O template, Path arithmetic and the Files API, Java serialization and its knobs, CSV parsing with quotes, and java.time — the types, arithmetic, zones and formatting.

How it works. Fifteen questions and three programs; 70% on the questions and every program accepted clears the module.

Before you start, make sure you can answer:

  • Why read() returns int, and what length() versus getBytes(UTF_8).length count.
  • The order of the decorator stack from a raw InputStream to readLine().
  • Why Scanner is slow, what buffering saves, and why a program with a PrintWriter printed nothing.
  • What resolve does with an absolute argument, and normalize versus toRealPath.
  • Which Files methods return lazy streams that must be closed.
  • What serialVersionUID and transient do, and why untrusted deserialisation is dangerous.
  • Why split(",") is not a CSV parser.
  • LocalDateTime versus ZonedDateTime versus Instant; Period versus Duration; what Jan 31 plusMonths(1) gives.

The programs are a log analyser that reads until end of input and measures the span with java.time, a hex dump of raw bytes, and a CSV-to-fixed-width report using the quote-aware parser.

Common questions

What is the decorator stack from a raw InputStream to readLine()?

A FileInputStream or System.in supplies bytes, an InputStreamReader decodes them into characters with a charset, and a BufferedReader batches the reads and adds readLine(). Files.newBufferedReader(path) builds the same stack, with UTF-8, in one call.

Which Java Files methods return lazy streams that must be closed?

Files.lines, Files.list, Files.walk and Files.find return streams backed by an open file or directory handle, so each one belongs in a try-with-resources block.

Why is split(",") not a CSV parser?

A quoted CSV field may itself contain commas, doubled quotes or line breaks, and split(",") cuts it in the wrong places. A correct parser tracks whether it is inside quotes and treats a doubled quote as an escaped one.

Exercises

Log analyser

Read log lines until end of input with a BufferedReader. Each line is <ISO date-time> <LEVEL> <message…>, for example 2024-03-01T10:15:00 ERROR disk full. Skip blank lines. Print:

lines=<count>
INFO=<n> WARN=<n> ERROR=<n> OTHER=<n>
first=<first timestamp> last=<last timestamp>
span=<minutes between first and last>m
firstError=<message of the first ERROR line, or ->

Use LocalDateTime.parse and Duration.between for the span. Assume at least one line.

Hex dump

Read all of standard input as raw bytes and print a hex dump, eight bytes per row: an 8-digit lower-case hex offset, two spaces, the bytes as two-digit hex separated by single spaces padded to 23 characters, two spaces, then the printable ASCII (0x20–0x7e) of the row between | bars with every other byte shown as .. Finally print total=<byte count>.

Example (input Hello, World! followed by a newline — 14 bytes) →

00000000  48 65 6c 6c 6f 2c 20 57  |Hello, W|
00000008  6f 72 6c 64 21 0a        |orld!.|
total=14

CSV to a fixed-width report

Read CSV lines name,qty,price until end of input; the name may be quoted and contain commas (RFC 4180 rules, doubled quotes for a literal quote). Print one line per record formatted %-14s%5d%10.2f (name left-aligned in 14, quantity right-aligned in 5, line total qty × price in 10 with two decimals), then a final line in the same format with the name TOTAL, the total quantity and the grand total. Use Locale.ROOT.

Example input

Widget,2,3.5
"Gear, large",1,10

→

Widget            2      7.00
Gear, large       1     10.00
TOTAL             3     17.00

In this module: I/O, files and time

← java.time — dates, times, zones and durations · The release train — what changed from Java 9 to 21 →