commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jasper Blues (JIRA)" <>
Subject [jira] Created: (IO-250) Add FileUtils.pathTo(File aFile, File fromAnotherFile)
Date Thu, 30 Sep 2010 08:36:34 GMT
Add FileUtils.pathTo(File aFile, File fromAnotherFile)

                 Key: IO-250
             Project: Commons IO
          Issue Type: New Feature
         Environment: n/a
            Reporter: Jasper Blues
            Priority: Minor

Please consider adding the following method to I've submitted
the method, with test cases below (rather than create a patch file). As a single method, it
should prove very simple to integrate. 

The method returns the path to a file, from another file, as described in the Javadoc method
header below: 

     * Returns the path of a aFile relative to another aFile, for example the location of
a file: 
     * <code>resources/language/english/</code> relative to
     * <code>resources/language/japanese/</code> is
     * <code>../../english/</code>
     * @param aFile           the aFile to check relative location
     * @param fromAnotherFile the base location
     * @return the relative location path
     * @throws on IO error
    public static String pathTo(File aFile, File fromAnotherFile) throws IOException {
        LOGGER.debug("Find path to file: " + aFile.toString() + " from file: " + fromAnotherFile.toString());
        Stack<File> fileToDirectories = directoriesFor(aFile);
        Stack<File> fileFromDirectories = directoriesFor(fromAnotherFile);

        while (fileToDirectories.peek().equals(fileFromDirectories.peek())) {
            if (fileToDirectories.isEmpty() || fileFromDirectories.isEmpty()) {

        StringBuilder pathToCommonParentDirectory = new StringBuilder();
        while (!fileFromDirectories.isEmpty()) {

        StringBuilder pathToFileFromCommonParentDirectory = new StringBuilder();
        while (!fileToDirectories.isEmpty()) {
            if (!fileToDirectories.isEmpty()) {
        return pathToCommonParentDirectory.toString() + pathToFileFromCommonParentDirectory.toString();

    private static Stack<File> directoriesFor(File file) throws IOException {
        Stack<File> pathElements = new Stack<File>();
        for (File element = file.getCanonicalFile(); element != null; element = element.getParentFile())
        return pathElements;

. . . this is useful for batch processing, web applications, etc. 

Test Cases: 

    public void pathTo() throws IOException {
        File file1 = new File("configs/js/en/a.xml");
        File file2 = new File("configs/js/ja/a.xml");

        Assert.assertEquals("../../en/a.xml", FileUtils.pathTo(file1, file2));

    public void pathTo_windowsStyleOnUnixMachine() throws IOException {
        File file1 = new File("c:/fred/foobar/");
        File file2 = new File("c:/data/zz.txt");

        Assert.assertEquals("../../fred/foobar/", FileUtils.pathTo(file1, file2));
        Assert.assertEquals("../../../data/zz.txt", FileUtils.pathTo(file2, file1));

    public void pathTo_fromParentDirectory() throws IOException {
        File file1 = new File("ui-performance-enhancer/out/test/ui-performance-enhancer/configs/css/imported.xml");
        File file2 = new File("ui-performance-enhancer/out/test/ui-performance-enhancer/configs/css");
        Assert.assertEquals("imported.xml", FileUtils.pathTo(file1, file2));


This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

View raw message