String (Iterable<? extends File> path, final boolean caseSensitiveFS) { final String fPath = myFile.getPath(); for (File dir: path) { String dirPath = dir.getPath(); if (dirPath.length() == 0) { dirPath = System.getProperty("user.dir"); } if (!fPath.regionMatches(!caseSensitiveFS, 0, dirPath, 0, dirPath.length())) { continue; } final int pathLength = fPath.length(); final boolean endsWithSeparator = dirPath.endsWith(File.separator); if (!endsWithSeparator) { // need to check if the next char in fPath is file separator final int separatorIdx = dirPath.length(); if (pathLength <= separatorIdx || fPath.charAt(separatorIdx) != File.separatorChar) { continue; } } // fPath starts with dirPath final int startIndex = endsWithSeparator ? dirPath.length() : dirPath.length() + 1; int endIndex = fPath.lastIndexOf('.'); if (endIndex <= startIndex) { endIndex = fPath.length(); } final int length = endIndex - startIndex; final StringBuilder buf = new StringBuilder(length).append(fPath, startIndex, endIndex); for (int idx = 0; idx < length; idx++) { if (buf.charAt(idx) == File.separatorChar) { buf.setCharAt(idx, '.'); } } return buf.toString(); } return null; }