Note that there are some explanatory texts on larger screens.

plurals
  1. POjava.nio.Path relativize between Paths does assumptions, which I cannot check
    primarykey
    data
    text
    <p>I am using the new Path object of java 7 and I am running into an issue.</p> <p>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.</p> <p>I have two usecases. 1.</p> <pre><code>Path baseDir = Paths.get("uploads"); Path filename = Paths.get("uploads/image/test.png") return baseDir.relativize(filename); </code></pre> <p>This returns a Path <code>image/test.png</code>, which is perfect.</p> <p>However, usecase 2:</p> <pre><code>Path baseDir = Paths.get("uploads"); Path filename = Paths.get("image/test.png") return baseDir.relativize(filename); </code></pre> <p>returns <code>../image/test.png</code>. I just want it to return "image/test.png"</p> <p>In the <a href="http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html#relativize" rel="nofollow">Path tutorial</a> it says </p> <blockquote> <p>In the absence of any other information, it is assumed that 2 Paths are siblings</p> </blockquote> <p>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.</p> <p>I currently solve it like this, but I was hoping there was a better way:</p> <pre><code>Path rootEnding = getRootDirectory().getName(getRootDirectory().getNameCount() - 1); for (Path part : path) { if (part.equals(rootEnding)) { return getRootDirectory().relativize(path); } } return path; </code></pre> <p>So my question is, is there any better way of checking this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload