Java 如何使用lastIndexOf方法提取具有文件名的立即目录的名称?

Java 如何使用lastIndexOf方法提取具有文件名的立即目录的名称?,java,Java,我想用文件名提取立即目录的名称。例如 c:/wallpers/images/images.gif我只想检索images/images.gif。我该怎么做 String shortenedPath(String path) { File complete = new File(path); String parentDir = complete.getParent().getName(); File shortened = new File(parentDir, complete.ge

我想用文件名提取立即目录的名称。例如 c:/wallpers/images/images.gif我只想检索images/images.gif。我该怎么做

String shortenedPath(String path) {
  File complete = new File(path);
  String parentDir = complete.getParent().getName();
  File shortened = new File(parentDir, complete.getName());
  return shortened.toString();
}