title: Absolute to Relative File Path in Java
body: <p>Given I have two <code>File</code> objects I can think of the following implementation:</p>
  <pre><code>public File convertToRelative(File home, File file) {
      final String homePath = home.getAbsolutePath();
      final String filePath = file.getAbsolutePath();
  
      // Only interested in converting file path that is a 
      // direct descendants of home path
      if (!filePath.beginsWith(homePath)) {
          return file;
      }
  
      return new File(filePath.substring(homePath.length()+1));
  }
  </code></pre>
  <p>Is there some smarter way of converting an absolute file path to a relative file path?</p>
  <blockquote>
  <p><strong>Possible Duplicate:</strong></p>
  <p><a href="https://stackoverflow.com/questions/204784/how-to-construct-a-relative-path-in-java-from-two-absolute-paths-or-urls">How to construct a relative path in java from two absolute paths or urls</a></p>
  </blockquote>