Courseiva
Java I/O API and Securing ApplicationsmediumMultiple ChoiceObjective-mapped

1Z0-829 Java I/O API and Securing Applications Practice Question

A developer needs to copy a large directory tree from one location to another, preserving file attributes. Which method should be used?

⚠ Common exam trap

Many candidates assume `Files.copy` with `COPY_ATTRIBUTES` works recursively on directories, but it only copies the directory entry, not its contents, requiring a tree-walking approach like `Files.walkFileTree` to achieve a full recursive copy with attribute preservation.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Implement a FileVisitor using Files.walkFileTree and copy each file with COPY_ATTRIBUTES

`Files.walkFileTree` with a custom `FileVisitor` allows you to recursively traverse a directory tree and copy each file individually using `Files.copy` with `StandardCopyOption.COPY_ATTRIBUTES`. This is necessary because `Files.copy` on a directory does not copy the directory tree recursively; it only copies the top-level directory entry. The `FileVisitor` pattern ensures that subdirectories and files are visited and copied in depth-first order, preserving file attributes at each step.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Files.copy(source, target, StandardCopyOption.COPY_ATTRIBUTES)

    Why it's wrong here

    Files.copy does not copy directories recursively.

  • Implement a FileVisitor using Files.walkFileTree and copy each file with COPY_ATTRIBUTES

    Why this is correct

    walkFileTree allows recursive traversal and attribute preservation.

  • Use Files.walk() and then Files.copy() for each entry

    Why it's wrong here

    Files.walk returns a stream; using Files.copy on directories fails.

  • Use FileUtils.copyDirectory from Apache Commons IO

    Why it's wrong here

    This is not part of standard Java SE; the question expects standard API.

About these practice questions

One of 513 original 1Z0-829 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.