Question: I am using the new Path object of java 7 and I am running into an issue.

I have a file storage system with a base directory and I create my own relative path. In the end I want to store just this relative path somewhere. I am running into a problem with Path.relativize though.

I have two usecases.
1.

```
Path baseDir = Paths.get("uploads");
Path filename = Paths.get("uploads/image/test.png")

return baseDir.relativize(filename);

```

This returns a Path `image/test.png`, which is perfect.

However, usecase 2:

```
Path baseDir = Paths.get("uploads");
Path filename = Paths.get("image/test.png")

return baseDir.relativize(filename);

```

returns `../image/test.png`. I just want it to return "image/test.png"

In the [Path tutorial](http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html#relativize) it says 

> 
> In the absence of any other information, it is assumed that 2 Paths are siblings
> 
> 
> 

What I want is to be able to detect that this is the case. In this case, I want to just return the filename and ignore the baseDir.

I currently solve it like this, but I was hoping there was a better way:

```
Path rootEnding = getRootDirectory().getName(getRootDirectory().getNameCount() - 1);

for (Path part : path) {
    if (part.equals(rootEnding)) {
        return getRootDirectory().relativize(path);
    }
}

return path;

```

So my question is, is there any better way of checking this?

Answer: 
Try adding a `normalize()` after `relativize()`. It seems to intended to do exactly this (remove unnecessary .. and . ). Don't miss the caution about symlinks in the javadoc.
This isn't 100% equivalent to what you wrote above, but I think it does what you want. Basically, let `baseDir` be a relative path. Pretend that whatever `baseDir` is relative to is the root of the file system. Then allow `filename` to be either relative or absolute from this "simulated root".

What about:

```
if (filename.startsWith(baseDir)) {
    filename = baseDir.relativize(filename);
}

```