FileUtils { public static String getRelativePath(File rootDirectory, File file) { try { String rootDir = rootDirectory.getCanonicalPath() + File.separator; String f = file.getCanonicalPath(); if (f.startsWith(rootDir)) { return f.substring(rootDir.length()); } } catch (IOException e) { } return file.getPath(); } private  FileUtils(); static String getRelativePath(File rootDirectory, File file); static String slashify(String path);  }
@Test public void test_getRelativePath_fileInDir() { File dir = new File("/a/b/c"); File f = new File("/a/b/c/d/foo.bar"); assertThat(getRelativePath(dir, f)).isEqualTo("d/foo.bar"); }
@Test public void test_getRelativePath_fileNotInDir() { File dir = new File("/a/b/c"); File f = new File("/d/e/f/foo.bar"); assertThat(getRelativePath(dir, f)).isEqualTo("/d/e/f/foo.bar"); }
@Test public void test_getRelativePath_sameStart() { File dir = new File("/a/b/c"); File f = new File("/a/b/c.txt"); assertThat(getRelativePath(dir, f)).isEqualTo("/a/b/c.txt"); }