StringUtils { public static String cleanPath(final String path) { if (path == null) { return null; } String pathToUse = replace(path, WINDOWS_FOLDER_SEPARATOR, FOLDER_SEPARATOR); final int prefixIndex = pathToUse.indexOf(":"); String prefix = EMPTY; if (prefixIndex != -1) { prefix = pathToUse.substring(0, prefixIndex + 1); pathToUse = pathToUse.substring(prefixIndex + 1); } if (pathToUse.startsWith(FOLDER_SEPARATOR)) { prefix = prefix + FOLDER_SEPARATOR; pathToUse = pathToUse.substring(1); } final String[] pathArray = delimitedListToStringArray(pathToUse, FOLDER_SEPARATOR); final List<String> pathElements = new ArrayList<>(); int tops = 0; for (int i = pathArray.length - 1; i >= 0; i--) { final String element = pathArray[i]; if (CURRENT_PATH.equals(element)) { } else if (TOP_PATH.equals(element)) { tops++; } else { if (tops > 0) { tops--; } else { pathElements.add(0, element); } } } for (int i = 0; i < tops; i++) { pathElements.add(0, TOP_PATH); } return prefix + collectionToDelimitedString(pathElements, FOLDER_SEPARATOR); } private  StringUtils(); static String cleanPath(final String path);  }
@Test public void testCleanPath() { final String result = StringUtils.cleanPath("/a/b/../d.txt"); assertEquals("/a/d.txt", result); }
@Test public void shouldCleanPathContainingMultipleDoubleDots() { final String result = StringUtils.cleanPath("/a/b/../../d.txt"); assertEquals("/d.txt", result); }