In Java, path.resolveSibling(other) resolves other against the parent of path. The result is a "sibling": a file or folder next to the original one. Paths.get("reports/2026/jan.txt").resolveSibling("feb.txt") is reports/2026/feb.txt.
It is a short form of path.getParent().resolve(other) that also works when there is no parent: then other itself is returned. Nothing is renamed on disk; use Files.move with the new path for that.
An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.resolveSibling(...), after import java.nio.file.Path;.
Syntax
Pass the new last name, as a String or a Path:
Java
Path jan = Paths.get("reports/2026/jan.txt");
Path feb = jan.resolveSibling("feb.txt"); // reports/2026/feb.txt
Path bak = jan.resolveSibling(jan.getFileName() + ".bak"); // reports/2026/jan.txt.bak
Call
What it does
p.resolveSibling("new.txt")
Same folder, new name.
p.resolveSibling(p.getFileName() + ".bak")
Backup name next to the original.
p.resolveSibling("sub").resolve(p.getFileName())
Same name in a sibling folder.
Since Java 7; both overloads are default methods of the interface from Java 11. The usual next step is Files.move(p, sibling) or Files.copy.
Outputs below are shown for Linux and macOS. On Windows, Java prints \ between names instead of /; the note above each output says when a result really differs.
Example 1 — Point to Files Next to a Monthly Report
Replace the file name of a report, add a sub-folder name, and move from one user folder to its neighbour.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathResolveSiblingBasic {
public static void main(String[] args) {
Path jan = Paths.get("reports/2026/jan.txt");
System.out.println("feb.txt -> " + jan.resolveSibling("feb.txt"));
System.out.println("archive/jan.txt -> " + jan.resolveSibling("archive/jan.txt"));
System.out.println("bob -> " + Paths.get("/home/anna").resolveSibling("bob"));
System.out.println("jan unchanged: " + jan);
}
}
Output
Run with java PathResolveSiblingBasic. Shown for Linux and macOS. Windows prints the same paths with \ between the names.
feb.txt -> reports/2026/feb.txt
archive/jan.txt -> reports/2026/archive/jan.txt
bob -> /home/bob
jan unchanged: reports/2026/jan.txt
How It Works
Same folder, new name. The parent reports/2026 is kept and only jan.txt is replaced by feb.txt.
Several names go below the parent. archive/jan.txt is joined onto reports/2026, so the result is one folder deeper than the original file.
Folders have siblings too. The parent of /home/anna is /home, so the sibling bob becomes /home/bob; the original jan path is untouched because paths are immutable.
Example 2 — Make a Backup Name and Change an Extension
Three everyday renames: append .bak, swap .png for .webp with a helper, and put a thumbnail in a sibling folder.
Build the new name as text.config.getFileName() + ".bak" turns the file name into a String and appends the suffix, and resolveSibling places it in /etc/shop.
Swap the extension. The helper cuts the name at its last dot and adds webp; the folder part is never touched, so only beach.png changes.
Sibling folder, same name.resolveSibling("thumbs") points to photos/thumbs, and resolve(photo.getFileName()) adds the original name inside it.
Example 3 — Handle Absolute, Empty, and Parentless Cases
See what happens with an absolute argument, an empty argument, and a bare file name, and why getParent().resolve() is not a safe replacement.
Run with java PathResolveSiblingEdges. Shown for Linux and macOS. Windows prints the same paths with \ between the names.
absolute: /srv/shared/jan.txt
empty: reports/2026
no parent: feb.txt
getParent().resolve -> NullPointerException
How It Works
Absolute replaces everything. /srv/shared/jan.txt starts at the root, so the parent of jan is ignored, exactly as with resolve.
Empty gives the parent. Resolving the empty path onto reports/2026 adds nothing, so the result is the folder that contains jan.txt.
Safe without a parent. jan.txt has no parent, so resolveSibling just returns feb.txt, while the hand-written getParent().resolve(...) calls a method on null and throws.
Frequently Asked Questions
It joins the argument onto the parent of the path, so it replaces the last name: reports/jan.txt becomes reports/feb.txt.
No. It only builds a new Path. Pass the old and new paths to Files.move to rename the file on disk.
resolveSibling returns the argument itself, so Paths.get("a.txt").resolveSibling("b.txt") is b.txt. getParent().resolve would throw NullPointerException.
Take the file name as a String, cut it at the last dot, add the new extension, and pass it to resolveSibling.
resolve joins onto the path itself; resolveSibling joins onto its parent. For a file path you almost always want resolveSibling to get another file in the same folder.