Java examples for File Path IO:Path
create a path from an array of components
import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; public class Main{ public static final String SEPARATOR = "/"; /**/*from w w w.j av a2 s . c o m*/ * create a path from an array of components * @param components * @return */ public static Path pathFromComponents(String[] components) { if (null == components || components.length == 0) { return null; } return new PathImpl(pathStringFromComponents(components)); } /** * create a path from an array of components * @param components * @return */ public static String pathStringFromComponents(String[] components) { if (null == components || components.length == 0) { return null; } return cleanPath(join(components, SEPARATOR)); } /** * Clean the path string by removing leading and trailing slashes and removing duplicate slashes. * @param path input path * @return cleaned path string */ public static String cleanPath(String path) { if (path.endsWith(SEPARATOR)) { path = path.replaceAll(SEPARATOR + "+$", ""); } if (path.startsWith(SEPARATOR)) { path = path.replaceAll("^" + SEPARATOR + "+", ""); } return path.replaceAll("/+", SEPARATOR); } private static String join(String[] components, String sep) { StringBuilder sb = new StringBuilder(); for (String component : components) { if (sb.length() > 0) { sb.append(sep); } sb.append(component); } return sb.toString(); } }