Java Path resolveSibling() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

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
CallWhat 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.

Library declarations
Java
// java.nio.file.Path (interface)
Path resolveSibling(Path other)
Path resolveSibling(String other)

Parameters

ParameterDescription
otherThe path to put next to this one. Relative paths are joined onto the parent; an absolute path is returned unchanged.

Return Value

Return ValueDescription
PathgetParent().resolve(other), or other when this path has no parent.

Compare with Similar Methods

Close relatives:

MethodHow it differs
resolve()Joins onto the path itself instead of its parent.
getParent()The folder resolveSibling works in; may be null.
Files.move()Actually renames the file on disk.

Java Examples

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);
    }
}

How It Works

  1. Same folder, new name. The parent reports/2026 is kept and only jan.txt is replaced by feb.txt.
  2. 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.
  3. 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.

Java
import java.nio.file.Path;
import java.nio.file.Paths;

public class PathResolveSiblingRename {
    static Path withExtension(Path p, String ext) {
        String name = p.getFileName().toString();
        int dot = name.lastIndexOf('.');
        String base = dot > 0 ? name.substring(0, dot) : name;
        return p.resolveSibling(base + "." + ext);
    }

    public static void main(String[] args) {
        Path config = Paths.get("/etc/shop/config.yml");
        System.out.println("backup: " + config.resolveSibling(config.getFileName() + ".bak"));
        Path photo = Paths.get("photos/beach.png");
        System.out.println("webp:   " + withExtension(photo, "webp"));
        System.out.println("thumb:  " + photo.resolveSibling("thumbs").resolve(photo.getFileName()));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Java
import java.nio.file.Path;
import java.nio.file.Paths;

public class PathResolveSiblingEdges {
    public static void main(String[] args) {
        Path jan = Paths.get("reports/2026/jan.txt");
        System.out.println("absolute: " + jan.resolveSibling("/srv/shared/jan.txt"));
        System.out.println("empty:    " + jan.resolveSibling(""));
        Path lone = Paths.get("jan.txt");
        System.out.println("no parent: " + lone.resolveSibling("feb.txt"));
        try {
            lone.getParent().resolve("feb.txt");
        } catch (NullPointerException e) {
            System.out.println("getParent().resolve -> " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Absolute replaces everything. /srv/shared/jan.txt starts at the root, so the parent of jan is ignored, exactly as with resolve.
  2. Empty gives the parent. Resolving the empty path onto reports/2026 adds nothing, so the result is the folder that contains jan.txt.
  3. 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.

Next: Path.startsWith()

Check whether a path begins with whole name elements, such as /home/anna.

startsWith() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful