How to construct a relative path in Java from two absolute paths (or URLs)?

前端 未结 22 2646
小蘑菇
小蘑菇 2020-11-22 10:30

Given two absolute paths, e.g.

/var/data/stuff/xyz.dat
/var/data

How can one create a relative path that uses the second path as its base?

22条回答
  •  日久生厌
    2020-11-22 11:02

    My version is loosely based on Matt and Steve's versions:

    /**
     * Returns the path of one File relative to another.
     *
     * @param target the target directory
     * @param base the base directory
     * @return target's path relative to the base directory
     * @throws IOException if an error occurs while resolving the files' canonical names
     */
     public static File getRelativeFile(File target, File base) throws IOException
     {
       String[] baseComponents = base.getCanonicalPath().split(Pattern.quote(File.separator));
       String[] targetComponents = target.getCanonicalPath().split(Pattern.quote(File.separator));
    
       // skip common components
       int index = 0;
       for (; index < targetComponents.length && index < baseComponents.length; ++index)
       {
         if (!targetComponents[index].equals(baseComponents[index]))
           break;
       }
    
       StringBuilder result = new StringBuilder();
       if (index != baseComponents.length)
       {
         // backtrack to base directory
         for (int i = index; i < baseComponents.length; ++i)
           result.append(".." + File.separator);
       }
       for (; index < targetComponents.length; ++index)
         result.append(targetComponents[index] + File.separator);
       if (!target.getPath().endsWith("/") && !target.getPath().endsWith("\\"))
       {
         // remove final path separator
         result.delete(result.length() - File.separator.length(), result.length());
       }
       return new File(result.toString());
     }
    

提交回复
热议问题