- public static final char SLASH = '/';
- public static final String SLASH_STRING = Character.toString(SLASH);
- public static final String EMPTY = "";
-
- /**
- * Split a path (with '/' separator) in an array of length 2, the first part
- * being the parent path (which could be either absolute or relative), the
- * second one being the last segment, (guaranteed to be without a '/').
- */
- public static String[] getParentPath(String path) {
- if (path == null)
- throw new IllegalArgumentException("Path cannot be null");
- if (path.length() == 0)
- throw new IllegalArgumentException("Path cannot be empty");
- checkDoubleSlash(path);
- int parentIndex = path.lastIndexOf(SLASH);
- if (parentIndex == path.length() - 1) {// trailing '/'
- path = path.substring(0, path.length() - 1);
- parentIndex = path.lastIndexOf(SLASH);
- }
-
- if (parentIndex == -1) // no '/'
- return new String[] { EMPTY, path };
-
- return new String[] { parentIndex != 0 ? path.substring(0, parentIndex) : "" + SLASH,
- path.substring(parentIndex + 1) };
- }
-