@Test public void testRemoveDoubleDotInDir () { assertEquals("/test/test2/", FileUtils.removeDoubleDotInDir("/test/test3/../test2/")); assertEquals("C:/Users/test/testdir/.game/terrain.png", FileUtils.removeDoubleDotInDir("C:/Users/test/testdir/.game/cache/../terrain.png")); }
public static String removeDoubleDotInDir(String path) { if (path == null) { throw new NullPointerException(PATH_CANNOT_NULL); } if (path.isEmpty()) { throw new IllegalArgumentException(PATH_CANNOT_EMPTY); } if (path.startsWith("../")) { throw new IllegalArgumentException("Cannot relativize paths starting with ../"); } boolean endsWithSlash = path.endsWith("/"); path = path.replace("\\", "/"); if (!path.contains("/")) { return path; } String[] array = path.split("/"); StringBuilder sb = new StringBuilder(); for (int i = 1; i < array.length; i++) { if (array[i].equals("..")) { array[i] = null; array[i-1] = null; } } for (String entry : array) { if (entry != null) { sb.append(entry + "/"); } } String res = sb.toString(); if (res.endsWith("/") && !endsWithSlash) { return res.substring(0, res.length() - 1); } return res; }
FileUtils { public static String removeDoubleDotInDir(String path) { if (path == null) { throw new NullPointerException(PATH_CANNOT_NULL); } if (path.isEmpty()) { throw new IllegalArgumentException(PATH_CANNOT_EMPTY); } if (path.startsWith("../")) { throw new IllegalArgumentException("Cannot relativize paths starting with ../"); } boolean endsWithSlash = path.endsWith("/"); path = path.replace("\\", "/"); if (!path.contains("/")) { return path; } String[] array = path.split("/"); StringBuilder sb = new StringBuilder(); for (int i = 1; i < array.length; i++) { if (array[i].equals("..")) { array[i] = null; array[i-1] = null; } } for (String entry : array) { if (entry != null) { sb.append(entry + "/"); } } String res = sb.toString(); if (res.endsWith("/") && !endsWithSlash) { return res.substring(0, res.length() - 1); } return res; } }
FileUtils { public static String removeDoubleDotInDir(String path) { if (path == null) { throw new NullPointerException(PATH_CANNOT_NULL); } if (path.isEmpty()) { throw new IllegalArgumentException(PATH_CANNOT_EMPTY); } if (path.startsWith("../")) { throw new IllegalArgumentException("Cannot relativize paths starting with ../"); } boolean endsWithSlash = path.endsWith("/"); path = path.replace("\\", "/"); if (!path.contains("/")) { return path; } String[] array = path.split("/"); StringBuilder sb = new StringBuilder(); for (int i = 1; i < array.length; i++) { if (array[i].equals("..")) { array[i] = null; array[i-1] = null; } } for (String entry : array) { if (entry != null) { sb.append(entry + "/"); } } String res = sb.toString(); if (res.endsWith("/") && !endsWithSlash) { return res.substring(0, res.length() - 1); } return res; } protected  FileUtils(); }
FileUtils { public static String removeDoubleDotInDir(String path) { if (path == null) { throw new NullPointerException(PATH_CANNOT_NULL); } if (path.isEmpty()) { throw new IllegalArgumentException(PATH_CANNOT_EMPTY); } if (path.startsWith("../")) { throw new IllegalArgumentException("Cannot relativize paths starting with ../"); } boolean endsWithSlash = path.endsWith("/"); path = path.replace("\\", "/"); if (!path.contains("/")) { return path; } String[] array = path.split("/"); StringBuilder sb = new StringBuilder(); for (int i = 1; i < array.length; i++) { if (array[i].equals("..")) { array[i] = null; array[i-1] = null; } } for (String entry : array) { if (entry != null) { sb.append(entry + "/"); } } String res = sb.toString(); if (res.endsWith("/") && !endsWithSlash) { return res.substring(0, res.length() - 1); } return res; } protected  FileUtils(); static String readFile(String path, Charset encoding); static List<String> readLines(String path, Charset charset); static void writeFile(String path, String content, Charset encoding); static void recursiveDeleteDirectory(File f); static void recursiveDeleteDirectory(File f, boolean deleteDir); static List<String> listFiles(File dir); static void listFiles(File dir, FileListIterator iterator); static String getUserHomeDir(); static String getAppHomeDir(String appName); static String removeDoubleDotInDir(String path); static File getRelativeFile(File target, File base); static String getDirectoryOfFilePath(String filePath); static void createWritableDirIfAbsent(String dirPath); }
FileUtils { public static String removeDoubleDotInDir(String path) { if (path == null) { throw new NullPointerException(PATH_CANNOT_NULL); } if (path.isEmpty()) { throw new IllegalArgumentException(PATH_CANNOT_EMPTY); } if (path.startsWith("../")) { throw new IllegalArgumentException("Cannot relativize paths starting with ../"); } boolean endsWithSlash = path.endsWith("/"); path = path.replace("\\", "/"); if (!path.contains("/")) { return path; } String[] array = path.split("/"); StringBuilder sb = new StringBuilder(); for (int i = 1; i < array.length; i++) { if (array[i].equals("..")) { array[i] = null; array[i-1] = null; } } for (String entry : array) { if (entry != null) { sb.append(entry + "/"); } } String res = sb.toString(); if (res.endsWith("/") && !endsWithSlash) { return res.substring(0, res.length() - 1); } return res; } protected  FileUtils(); static String readFile(String path, Charset encoding); static List<String> readLines(String path, Charset charset); static void writeFile(String path, String content, Charset encoding); static void recursiveDeleteDirectory(File f); static void recursiveDeleteDirectory(File f, boolean deleteDir); static List<String> listFiles(File dir); static void listFiles(File dir, FileListIterator iterator); static String getUserHomeDir(); static String getAppHomeDir(String appName); static String removeDoubleDotInDir(String path); static File getRelativeFile(File target, File base); static String getDirectoryOfFilePath(String filePath); static void createWritableDirIfAbsent(String dirPath);  }