1Z0-819 I/O Operations 3 — Questions and Answers
Question 1: Which `Files` method reads all lines of a text file into a `List<String>` using the default charset?
- Files.readAllLines(path) (Correct answer)
- Files.lines(path)
- Files.readString(path)
- Files.readAllBytes(path)
Correct answer: Files.readAllLines(path)
`Files.readAllLines(path)` reads all lines into a `List<String>` using UTF-8 by default in Java 11.
Question 2: What is the key difference between `Files.lines(path)` and `Files.readAllLines(path)`?
- `Files.lines` returns a lazy `Stream<String>` while `readAllLines` loads everything into memory immediately (Correct answer)
- `Files.lines` reads bytes and `readAllLines` reads characters
- `Files.readAllLines` is asynchronous
- `Files.lines` requires explicit charset specification
Correct answer: `Files.lines` returns a lazy `Stream<String>` while `readAllLines` loads everything into memory immediately
`Files.lines` is lazy and streams lines on demand, whereas `readAllLines` materializes all lines into a `List` at once.
Question 3: Which method creates all missing parent directories for a given path in one call?
- Files.createDirectories(path) (Correct answer)
- Files.createDirectory(path)
- Files.mkdirs(path)
- new File(path).mkdirs()
Correct answer: Files.createDirectories(path)
`Files.createDirectories` creates the target directory and all missing parents without throwing an error if they already exist.
Question 4: What does `Path.normalize()` do?
- Removes redundant `.` and `..` elements from the path (Correct answer)
- Converts the path to an absolute path
- Resolves symlinks to the real path
- Converts backslashes to forward slashes
Correct answer: Removes redundant `.` and `..` elements from the path
`normalize()` eliminates redundant `.` (current directory) and `..` (parent directory) segments without accessing the filesystem.
Question 5: Which method on `Path` combines the current path with another path segment?
- resolve() (Correct answer)
- relativize()
- subpath()
- toAbsolutePath()
Correct answer: resolve()
`resolve()` appends the given path to the base path, handling absolute/relative combinations correctly.
Question 6: What does `Files.isSameFile(path1, path2)` check?
- Whether both paths locate the same file on the filesystem, even via different names or symlinks (Correct answer)
- Whether both paths are string-equal
- Whether both files have identical content
- Whether both files have the same last-modified timestamp
Correct answer: Whether both paths locate the same file on the filesystem, even via different names or symlinks
`Files.isSameFile` uses the OS to determine if both paths refer to the same inode/file, resolving symlinks.
Question 7: What happens when you call `Files.delete(path)` on a non-empty directory?
- It throws `DirectoryNotEmptyException` (Correct answer)
- It deletes the directory and all contents
- It silently does nothing
- It throws `IOException` with no specific subtype
Correct answer: It throws `DirectoryNotEmptyException`
`Files.delete` will throw `DirectoryNotEmptyException` if the directory still contains entries.
Which `Files` method reads all lines of a text file into a `List` using the default charset?