code: public static String getCanonicalDirectoryName(String path) {
          String realPath = getCanonicalPath(path);
          if (realPath == null) {
              return null;
          }
  
          String separator = System.getProperty("file.separator", "/");
          // backslashes must be escaped for split().  kthxbai
          separator = Pattern.quote(separator);
          String[] pathElements = realPath.split(separator);
          return pathElements.length > 0 ? pathElements[pathElements.length - 1] : realPath;
      }
docs: Gets the directory name of the canonical path -- the last element in the result of
  {@link #getCanonicalPath(String)}.
  @param path path to canonicalize
  @return canonical directory name