Skip to content
Ameen Abozaid
BEG-034

Files and I/O

  • 6 Lessons
  • 67 min of reading
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Paths
  • Files API
  • reading/writing text
  • streams
  • buffered I/O

Learn first

67 min of reading · 3 h of practice (estimate)

Learning outcomes

  1. Build and resolve paths with Path and check existence with Files.
  2. Read and write text files with Files.readAllLines, Files.writeString, and buffered readers/writers.
  3. Explain the difference between byte streams and character streams and when encoding matters.
  4. Handle I/O errors and always close resources.

Lessons

  1. 01Paths and the Files API11 min read
  2. 02Reading and writing text10 min read
  3. 03Streams, readers, and buffering11 min read
  4. 04Character encoding (UTF-8)12 min read
  5. 05A simple CSV format11 min read
  6. 06Demo: saving and loading contacts as CSV12 min read

Lab and exercises

Lab

BEG-034-LAB1 · Contacts persistence

Outcomes: 1–4 · Time: ~2 h Save contacts to CSV on exit and load them on start. Survive a missing or corrupt file. Use UTF-8 and relative paths. Acceptance: data survives restarts; bad lines reported, not fatal.

Exercises (assessment: three exercises)

IDOutcomeTask
BEG-034-EX011, 2Create a data folder if missing, write three lines to data/notes.txt, read them back and print them with line numbers.
BEG-034-EX022, 4Write a method that copies a text file line by line with BufferedReader and BufferedWriter in try-with-resources, printing a clear message on IOException.
BEG-034-EX033Explain when to use a byte stream vs a character stream, and what goes wrong if the encoding differs between writing and reading.
Show solutions
  • EX01 Files.createDirectories(Path.of("data")); Files.write(p, List.of("a","b","c")); then List<String> ls = Files.readAllLines(p); for (int i=0;i<ls.size();i++) System.out.println((i+1)+": "+ls.get(i));
  • EX02 try (var in = Files.newBufferedReader(src); var out = Files.newBufferedWriter(dst)) { String l; while ((l = in.readLine()) != null) { out.write(l); out.newLine(); } } catch (IOException e) { System.out.println("Copy failed: " + e.getMessage()); }
  • EX03 Byte streams for binary data (images, archives); character streams for text. Different encodings produce garbled characters (for example Arabic letters turn into symbols), so always use UTF-8 explicitly.
  • Outcome 4 (always close) is also checked by the lab and quiz Q09.

Quiz

Outcomes: O1 Path and Files · O2 reading and writing text · O3 byte vs character streams, encoding · O4 errors and closing.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Creating a Path object:O1
  2. 2Check whether a file exists:O1
  3. 3Which path style travels well between computers?O1
  4. 4Files.readAllLines(path) returns:O2
  5. 5Files.writeString(path, text) by default:O2
  6. 6BufferedReader is useful because it:O2
  7. 7Raw binary files such as images are best handled with:O3
  8. 8The recommended text encoding is:O3
  9. 9The safest way to ensure a file is closed:O4
  10. 10IOException is:O4

Practical part

Three exercises BEG-034-EX01 to EX03 plus the lab. Pass: all three exercises correct and the lab survives a missing file and a corrupt line.

Common mistakes

Hard-coded absolute paths; platform-default encoding; unclosed files.

Assessment

3 exercises.

Career relevance

File uploads (JNR-023) and logs (JNR-006).

Type to search levels, phases, modules and lessons.