java.nio.file Path/Files
Work with java.nio.file Path and Files: create, check, and delete files.
java.nio.file Path/Files is a free Java Academy lesson on CoddyKit — lesson 1 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
The java.nio.file package offers modern classes for file operations. Use Path to locate files and Files for actions.
Path basics
Path represents file or directory locations. Use Paths.get() to create paths.
Code: create Path
This code creates a Path to example.txt and prints its absolute path.
public class Main {
public static void main(String[] args) {
java.nio.file.Path p = java.nio.file.Paths.get("example.txt");
System.out.println("Path: " + p.toAbsolutePath());
}
}
Files utility
The Files class has static methods for existence checks, creation, deletion, copying, and more.
Code: exists()
Files.exists() quickly checks if a path exists.
public class Main {
public static void main(String[] args) throws java.io.IOException {
java.nio.file.Path p = java.nio.file.Paths.get("example.txt");
boolean exists = java.nio.file.Files.exists(p);
System.out.println("Exists? " + exists);
}
}
Code: create/delete
You can create and delete files with one line each using Files.createFile and Files.delete.
public class Main {
public static void main(String[] args) throws java.io.IOException {
java.nio.file.Path p = java.nio.file.Paths.get("temp.txt");
java.nio.file.Files.createFile(p);
System.out.println("Created temp.txt");
java.nio.file.Files.delete(p);
System.out.println("Deleted temp.txt");
}
}
Files.exists check
Quick check: What does Files.exists(path) return?
Recap
Recap: Path locates files, Files performs actions. Use them for safe file checks, creation, and deletion.
Frequently asked questions
Is the “java.nio.file Path/Files” lesson free?
Yes — the full text of “java.nio.file Path/Files” 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 “java.nio.file Path/Files”?
Work with java.nio.file Path and Files: create, check, and delete 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 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “java.nio.file Path/Files” 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
- java.nio.file Path/Files
- Reading/Writing Text Files
- Buffered Streams