Java Paths
Build a Path with Paths.get so you can name a place without opening the file yet. Open Route.java, call Paths.get("note.txt"), and print getFileName(). Compile with javac Route.java and run java Route. The screen should show note.txt. A second demo joins "docs" and "note.txt", prints the file name, then prints the parent folder. A third demo prints the whole joined path. Leave inheritance for the next page.
Name one file piece
The writing page stored letters with FileWriter and a plain string "note.txt". This page turns that kind of name into a Path object you can ask questions about. Open a teaching file named Route.java. Keep the public class name Route so the file name matches. Delete leftover Store write demos, leftover Reader scanner demos, leftover Signal throw lines, and any keyboard Scanner. The program must not wait for typing. You do not need to create note.txt on disk for these demos — naming a path is not the same as opening the file.
Near the top, keep:
import java.nio.file.Path;
import java.nio.file.Paths;
Inside main, build a path from one name:
Path p = Paths.get("note.txt");
Print only the last piece:
System.out.println(p.getFileName());
getFileName returns a Path that prints as note.txt. You are not writing tea and you are not reading oak. You are naming.
import java.nio.file.Path;
import java.nio.file.Paths;
public class Route {
public static void main(String[] args) {
Path p = Paths.get("note.txt");
System.out.println(p.getFileName());
}
}
From the folder where you keep Route.java, run javac Route.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Route — not java Route.java. Leave the .java ending off the run command. If javac cannot find Path or Paths, the import lines are missing or mistyped — put them back before you chase other errors.
The screen shows note.txt. That is the last piece of the path. The program should not pause for keyboard input. If the terminal stays empty with no print, you may still be running a Store write file that never calls println — switch to java Route. If you see tea or oak as file content stories, you mixed writing or reading into this naming lesson. If you see blocked or after, a Guard leftover is still launching.
Walk the run once. Java starts at Route.main. Paths.get builds a path from "note.txt". getFileName picks the last piece. println shows note.txt. Nothing is opened on disk. A missing file does not stop these lines, because you never opened the file. That quiet naming is the point of this first demo: a path can exist as a value even when no text file was prepared beside the class.
Join a folder name and a file name
A second program builds a path from two pieces: folder docs, then file note.txt. Print the file name first, then the parent.
import java.nio.file.Path;
import java.nio.file.Paths;
public class Route {
public static void main(String[] args) {
Path p = Paths.get("docs", "note.txt");
System.out.println(p.getFileName());
System.out.println(p.getParent());
}
}
Compile with javac Route.java, then run java Route. The first line is note.txt. The second line is docs. getFileName still gives only the last piece. getParent gives the folder piece that sits above it. Do not swap those meanings: the parent is not note.txt, and the file name is not docs.
Paths.get with more than one string joins the pieces in order. You do not type the separator by hand in this teaching call — Paths.get builds the path for you. The joined path still does not create a folder or a file by itself. Naming stays separate from writing and reading.
If getParent printed null on a single-name path in an experiment, that is because a lone "note.txt" has no folder piece in that path object. The two-piece demo keeps a clear parent so the screen shows docs.
Print the whole joined path
A third program keeps Paths.get("docs", "note.txt") and prints the path object itself.
import java.nio.file.Path;
import java.nio.file.Paths;
public class Route {
public static void main(String[] args) {
Path p = Paths.get("docs", "note.txt");
System.out.println(p);
}
}
Compile and run again. The screen shows the joined path as one value — often docs\note.txt or docs/note.txt depending on the system separator. The teaching point is that the pieces are joined, not that you memorize one slash character. Prefer reading getFileName and getParent when you want those pieces alone. If both lines from the previous demo still feel clearer, keep that two-line file nearby while you study the single joined print.
Do not invent inheritance on this page. You are still inside the files path of the course. The next page starts inheritance with classes that extend other classes. Also do not paste FileWriter into Route.java as a requirement here — writing already had its own lesson. Keep each Errors and Files topic in its own teaching class name so launches stay honest.
Compared with Store and Reader
On the writing page, java Store put letters into note.txt. On the reading page, java Reader printed a line from disk. On this page, java Route only names places with Path and Paths.get. Keep Store and Reader nearby only as history when you want to open the file again; run java Route for the path demos.
Paths.get, getFileName, and getParent are the new tokens. Import java.nio.file.Path and java.nio.file.Paths. You do not need FileWriter or Scanner for these naming demos.
What this leaves for inheritance
This page names paths. The next page begins inheritance, where one class can extend another. Stay with Paths.get and the file-name versus parent split here. Do not add extends as a requirement on these Route demos yet.
Before inheritance
You can build Paths.get("note.txt") and see note.txt from getFileName. You can join "docs" and "note.txt", see note.txt, then see parent docs. You can print the whole joined path as one value. Compile Route.java and run java Route for each kept demo in turn. The next page is inheritance, where classes share a parent type by extension.
Keep the single-name demo for a clear getFileName result. Keep the two-piece demo for a clear parent folder. Keep the joined print when you want one screen line. Keep inheritance off this page. Those habits are the checkpoint before you move on. When note.txt and docs appear on the two-line parent demo of java Route, paths did their job for this lesson and you are ready for the next topic about inheritance.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top