Odpowiedzi:
Stosowanie File„s getParentFile()metody i String.lastIndexOf()odzyskać tylko natychmiastowe katalogu nadrzędnego.
Komentarz Marka jest lepszym rozwiązaniem niż lastIndexOf():
file.getParentFile().getName();
Te rozwiązania działają tylko wtedy, gdy plik ma plik nadrzędny (np. Utworzony za pomocą jednego z konstruktorów plików biorących udział w programie File). Kiedy getParentFile()jest null, musisz skorzystać z lastIndexOflub użyć czegoś takiego jak Apache Commons 'FileNameUtils.getFullPath() :
FilenameUtils.getFullPathNoEndSeparator(file.getAbsolutePath());
=> C:/aaa/bbb/ccc/ddd
Istnieje kilka wariantów zachowania / usunięcia przedrostka i separatora końcowego. Możesz użyć tej samej FilenameUtilsklasy, aby pobrać nazwę z wyniku, użyć lastIndexOfitp.
lastIndexOf, po prostu użyj file.getParentFile().getName().
null(jeśli utworzyłeś Fileinstancję ze ścieżką względną) - spróbuj file.getAbsoluteFile().getParentFile().getName().
File f = new File("C:/aaa/bbb/ccc/ddd/test.java");
System.out.println(f.getParentFile().getName())
f.getParentFile() może mieć wartość null, więc powinieneś to sprawdzić.
Użyj poniżej,
File file = new File("file/path");
String parentPath = file.getAbsoluteFile().getParent();
Jeśli masz tylko ścieżkę String i nie chcesz tworzyć nowego obiektu File, możesz użyć czegoś takiego:
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"
jeśli chcesz dołączyć folder „ddd” inną ścieżką, użyj;
String currentFolder= "/" + currentPath.getName().toString();
Z java 7 wolałbym używać Path. Musisz tylko wprowadzić ścieżkę do:
Path dddDirectoryPath = Paths.get("C:/aaa/bbb/ccc/ddd/test.java");
i utwórz metodę get:
public String getLastDirectoryName(Path directoryPath) {
int nameCount = directoryPath.getNameCount();
return directoryPath.getName(nameCount - 1);
}
W Groovy:
Nie ma potrzeby tworzenia Fileinstancji, aby przeanalizować ciąg w groovy. Można to zrobić w następujący sposób:
String path = "C:/aaa/bbb/ccc/ddd/test.java"
path.split('/')[-2] // this will return ddd
Podział utworzy tablicę, [C:, aaa, bbb, ccc, ddd, test.java]a indeks -2wskaże wpis przed ostatnim, którym w tym przypadku jestddd
//get the parentfolder name
File file = new File( System.getProperty("user.dir") + "/.");
String parentPath = file.getParentFile().getName();
test.javaprawdopodobnie nawet nie będzie istnieć na komputerze, na którym program jest uruchamiany. To skompilowany.classplik, który jest uruchamiany. Więc to zadziała tylko wtedy, gdy wiesz, gdzie siędddznajduje, w takim przypadku nie ma sensu szukać go programowo; po prostu mocno go zakoduj.