File Walking & Utilities
Explore java.nio utilities: walking directories, listing files, and copying/moving files.
File Walking & Utilities is a free Java Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
File utilities in java.nio let you list, copy, move, and delete files. Files.walk() explores directories.
Files.list
Files.list(path) lists entries in a directory (non-recursive). Useful for quick checks.
Code: list dir
This code lists files in the current directory.
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
Path dir = Paths.get(".");
java.nio.file.Files.list(dir)
.forEach(System.out::println);
}
}
Files.walk
Files.walk(path) traverses a directory tree recursively, returning a stream of paths.
Code: walk tree
This walks 2 levels deep in the current directory and prints paths.
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
Path dir = Paths.get(".");
java.nio.file.Files.walk(dir, 2)
.forEach(System.out::println);
}
}
Code: copy/move
Files.copy and Files.move handle file duplication and renaming easily.
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
Path src = Paths.get("a.txt");
Path dest = Paths.get("b.txt");
java.nio.file.Files.copy(src, dest, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Copied a.txt to b.txt");
java.nio.file.Files.move(dest, Paths.get("c.txt"), StandardCopyOption.REPLACE_EXISTING);
System.out.println("Moved b.txt to c.txt");
}
}
walk check
Quick check: Which method can recursively walk a file tree?
Recap
Recap: Use Files.list for a single folder, Files.walk for recursive traversal, and Files.copy/move for file operations.
Frequently asked questions
Is the “File Walking & Utilities” lesson free?
Yes — the full text of “File Walking & Utilities” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “File Walking & Utilities”?
Explore java.nio utilities: walking directories, listing files, and copying/moving files. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “File Walking & Utilities” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Serialization Basics
- Simple CSV/JSON Handling
- File Walking & Utilities