1Z0-771 study materials
1Z0-771 study resources

1Z0-771: I/O, NIO.2, and Serialization

1Z0-771 · Work through your exam, one topic at a time.

All guide sections

I/O, NIO.2, and Serialization

Domain 7 tests the java.io stream hierarchy, the java.nio.file path API, and the serialization contract: which classes are byte- versus character-oriented, and what makes an object serializable.

I/O streams

  • Byte streams (InputStream/OutputStream) handle binary data; character streams (Reader/Writer) handle text with a charset.
  • BufferedReader.readLine() returns null at end of stream; InputStream.read() returns -1. Always close with try-with-resources.
  • File is the legacy API; Path plus Files replaces it.

NIO.2

  • Path.of("a", "b") (or Paths.get) builds a path; resolve appends, relativize computes the difference between two paths, and normalize removes . and .. textually without touching the disk.
  • Files.exists, isDirectory, createDirectories, copy, move, delete, readAllLines, lines, walk, list.
  • Files.walk and Files.list return lazy Stream<Path> objects that must be closed.

Serialization

  • A class must implement the marker interface java.io.Serializable; non-transient, non-static fields are written.
  • transient fields are skipped and restored to default values. A serialVersionUID mismatch throws InvalidClassException.
  • A non-serializable field throws NotSerializableException unless it is transient.
  • During deserialization, constructors of serializable classes do not run; the no-arg constructor of the first non-serializable superclass does.

Worked example

try (var out = new ObjectOutputStream(new FileOutputStream("d.bin"))) {
    out.writeObject(new Point(1, 2));
}

Point must implement Serializable; otherwise writeObject throws NotSerializableException at runtime, and the file may already contain partial bytes.

Traps

  • Files.delete throws NoSuchFileException; deleteIfExists returns a boolean.
  • Files.copy fails if the target exists unless REPLACE_EXISTING is passed.
  • Closing only the outer wrapper closes the entire chain.