public static String getParent(String path) {
        String parent = null;
        int lastIndex = path.lastIndexOf('/');
        if (lastIndex != -1) {
            if (path.length() == 1) {
                parent = null;
            } else if (lastIndex == 0) {
                parent = "/";
            } else {
                parent = path.substring(0, lastIndex);
            }
        }
        return parent;

    }
public static String getParent ( String path ) { String parent = null ; int lastIndex = path . lastIndexOf ( '/' ) ; if ( lastIndex != - 1 ) { if ( path . length ( ) == 1 ) { parent = null ; } else if ( lastIndex == 0 ) { parent = "/" ; } else { parent = path . substring ( 0 , lastIndex ) ; } } return parent ; }
Answer the path with the last file name removed, using forward
slash ('/') as the path separator character.

Answer null for a path which contains no slashes.

Answer null for a path which is a single slash character.

Answer the path which is a single slash character when
the path starts with a slash, which is not a single slash
character, and which contains no other slashes.

Answer the path with the trailing slash removed for
a path which has a trailing slash and which is not a single
slash character.

Answer the the path up to but not including the trailing
slash in all other cases.

For example:

For "/grandParent/parent/child" answer "/grandParent/parent".

For "/grandParent/parent/" answer "/grandParent/parent".

For "/parent" answer "/".

For "/" answer null.

For "child" answer null.

For "" answer null.

@param path The path with the last file named removed. An exception
will be thrown if the path is null.

@return The path with the last file name removed.