回答:
使用FileのgetParentFile()方法およびString.lastIndexOf()取得するためだけの直接の親ディレクトリを。
マークさんのコメントは、より優れたソリューションですlastIndexOf()。
file.getParentFile().getName();
これらのソリューションは、ファイルに親ファイルがある場合にのみ機能します(たとえば、親をとるファイルコンストラクターの1つを介して作成されるFile)。ときにgetParentFile()あなたが使用してに頼る必要がありますnullの場合lastIndexOf、または使用のようなものApacheのコモンズのFileNameUtils.getFullPath():
FilenameUtils.getFullPathNoEndSeparator(file.getAbsolutePath());
=> C:/aaa/bbb/ccc/ddd
接頭辞と末尾のセパレータを保持/削除するいくつかのバリアントがあります。同じFilenameUtilsクラスを使用して結果から名前を取得するかlastIndexOf、などを使用できます。
lastIndexOf、使用してくださいfile.getParentFile().getName()。
                    nullれたFile場合(相対パスでインスタンスを作成した場合)-を試してくださいfile.getAbsoluteFile().getParentFile().getName()。
                    File f = new File("C:/aaa/bbb/ccc/ddd/test.java");
System.out.println(f.getParentFile().getName())
f.getParentFile() nullの可能性があるため、チェックする必要があります。
以下を使用して、
File file = new File("file/path");
String parentPath = file.getAbsoluteFile().getParent();
              文字列パスだけがあり、新しいFileオブジェクトを作成したくない場合は、次のようなものを使用できます。
public static String getParentDirPath(String fileOrDirPath) {
    boolean endsWithSlash = fileOrDirPath.endsWith(File.separator);
    return fileOrDirPath.substring(0, fileOrDirPath.lastIndexOf(File.separatorChar, 
            endsWithSlash ? fileOrDirPath.length() - 2 : fileOrDirPath.length() - 1));
}
              File file = new File("C:/aaa/bbb/ccc/ddd/test.java");
File curentPath = new File(file.getParent());
//get current path "C:/aaa/bbb/ccc/ddd/"
String currentFolder= currentPath.getName().toString();
//get name of file to string "ddd"
別のパスを使用してフォルダ「ddd」を追加する必要がある場合。
String currentFolder= "/" + currentPath.getName().toString();
              Java 7以降では、Pathを使用したいと思います。あなただけにパスを入れる必要があります:
Path dddDirectoryPath = Paths.get("C:/aaa/bbb/ccc/ddd/test.java");
そしていくつかのgetメソッドを作成します:
public String getLastDirectoryName(Path directoryPath) {
   int nameCount = directoryPath.getNameCount();
   return directoryPath.getName(nameCount - 1);
}
                  //get the parentfolder name
    File file = new File( System.getProperty("user.dir") + "/.");
    String parentPath = file.getParentFile().getName();
              
test.javaおそらくプログラムが実行されているコンピューター上に存在しません。実行されるのはコンパイルされた.classファイルです。したがって、これはddd、場所がわかっている場合にのみ機能します。その場合、プログラムで検索しても意味がありません。ただハードコーディングしてください。