Java Utililty Methods Path Relative

List of utility methods to do Path Relative

Description

The list of methods to do Path Relative are organized into topic(s).

Method

StringgetRelativeFileName(String filePath, String directoryPath)
get Relative File Name
String relativeFileName = filePath;
if (directoryPath != null) {
    relativeFileName = filePath.substring(directoryPath.length());
    if (relativeFileName.startsWith(File.separator))
        relativeFileName = relativeFileName.substring(1);
return relativeFileName;
FilegetRelativeFilePath(File absolutePath, File reference)
get Relative File Path
File relativePath = absolutePath;
if (absolutePath.getAbsolutePath().contains(reference.getAbsolutePath())) {
    relativePath = new File(
            absolutePath.getAbsolutePath().substring(reference.getAbsolutePath().length() + 1));
return relativePath;
StringgetRelativeFilePath(String filePath, String relativePathPrefix)
Get relative path that follows relativePathPrefix .
int prefixIndex = filePath.indexOf(relativePathPrefix);
if (prefixIndex == -1)
    return filePath;
int relativePathStartIndex = prefixIndex + relativePathPrefix.length() + 1;
return filePath.substring(relativePathStartIndex);
FilegetRelativeLastModifiedTimeFile()
get Relative Last Modified Time File
return new File(LAST_MODIFIED_FILE_NAME);
StringgetRelativeLink(File target, File base)
Returns the path of one File relative to another.
if (base == null) {
    return getFullPath(target);
} else {
    return getRelativePath(target, base);
StringgetRelativeName(File file, File root)
Strips off the part of the path before the start of the root file.
int length = root.getAbsolutePath().length();
String absolutePath = file.getAbsolutePath();
String relativePath;
if (absolutePath.length() == length) {
    if (file.getAbsolutePath().equals(root.getAbsolutePath())) {
        relativePath = "";
    } else {
        throw new RuntimeException(
...
StringgetRelativeName(final File directory, final File file)
get Relative Name
final String name = directory.toURI().relativize(file.toURI()).getPath();
if (file.isDirectory()) {
    return name.endsWith("/") ? name : name + "/";
return name;
StringgetRelativeName(final File file)
Strips the leading path up to "config".
String absName = file.getAbsolutePath();
int inx = absName.indexOf("config");
if (inx != -1) {
    return absName.substring(inx, absName.length());
return null;
StringgetRelativeParentDirectory(File base, File file)
get Relative Parent Directory
return getRelativePath(base, new File(getAbsoluteParentDirectory(file)));
StringgetRelativeTemporaryFilename(String directory, String suffix, boolean autodelete)
get Relative Temporary Filename
int num = Math.abs(rng.nextInt());
File f = new File(directory + File.separator + String.format("%06d.%s", num, suffix));
while (f.exists()) {
    num = Math.abs(rng.nextInt());
    f = new File(directory + File.separator + String.format("%06d.%s", num, suffix));
f.createNewFile();
if (autodelete)
...