How to construct a relative path in Java from two absolute paths (or URLs)?
Как построить относительный путь в Java из двух абсолютных путей (или URL-адресов)?
Учитывая два абсолютных пути, например
/var/data/stuff/xyz.dat /var/data
Как можно создать относительный путь, который использует второй путь в качестве основы? В приведенном выше примере результат должен быть: ./stuff/xyz.dat
Переведено автоматически
Ответ 1
Это небольшой обходной путь, но почему бы не использовать URI? У него есть метод relativize, который выполняет все необходимые проверки за вас.
На момент написания (июнь 2010 г.) это было единственное решение, которое прошло мои тестовые примеры. Я не могу гарантировать, что это решение не содержит ошибок, но оно проходит включенные тестовые примеры. Метод и тесты, которые я написал, зависят от FilenameUtils класса из Apache commons IO.
Решение тестировалось на Java 1.4. Если вы используете Java 1.5 (или выше), вам следует рассмотреть возможность замены StringBuffer на StringBuilder (если вы все еще используете Java 1.4, вам следует подумать о смене работодателя).
/** * Get the relative path from one file to another, specifying the directory separator. * If one of the provided resources does not exist, it is assumed to be a file unless it ends with '/' or * '\'. * * @param targetPath targetPath is calculated to this file * @param basePath basePath is calculated from this file * @param pathSeparator directory separator. The platform default is not assumed so that we can test Unix behaviour when running on Windows (for example) * @return */ publicstatic String getRelativePath(String targetPath, String basePath, String pathSeparator) {
// Normalize the paths StringnormalizedTargetPath= FilenameUtils.normalizeNoEndSeparator(targetPath); StringnormalizedBasePath= FilenameUtils.normalizeNoEndSeparator(basePath);
// Undo the changes to the separators made by normalization if (pathSeparator.equals("/")) { normalizedTargetPath = FilenameUtils.separatorsToUnix(normalizedTargetPath); normalizedBasePath = FilenameUtils.separatorsToUnix(normalizedBasePath);
if (commonIndex == 0) { // No single common path element. This most // likely indicates differing drive letters, like C: and D:. // These paths cannot be relativized. thrownewPathResolutionException("No common path element found for '" + normalizedTargetPath + "' and '" + normalizedBasePath + "'"); }
// The number of directories we have to backtrack depends on whether the base is a file or a dir // For example, the relative path from // // /foo/bar/baz/gg/ff to /foo/bar/baz // // ".." if ff is a file // "../.." if ff is a directory // // The following is a heuristic to figure out if the base refers to a file or dir. It's not perfect, because // the resource referred to by this path may not actually exist, but it's the best I can do booleanbaseIsFile=true;
FilebaseResource=newFile(normalizedBasePath);
if (baseResource.exists()) { baseIsFile = baseResource.isFile();