如何只获取特定文件的父目录名称

时间:2021-09-19 03:20:20

How to get ddd from the path name where the test.java resides.

如何从test.java所在的路径名获取ddd。

File file = new File("C:/aaa/bbb/ccc/ddd/test.java");

8 个解决方案

#1


107  

Use File's getParentFile() method and String.lastIndexOf() to retrieve just the immediate parent directory.

使用File的getParentFile()方法和String.lastIndexOf()仅检索直接父目录。

Mark's comment is a better solution thanlastIndexOf():

Mark的评论比popIndexOf()更好:

file.getParentFile().getName();

These solutions only works if the file has a parent file (e.g., created via one of the file constructors taking a parent File). When getParentFile() is null you'll need to resort to using lastIndexOf, or use something like Apache Commons' FileNameUtils.getFullPath():

这些解决方案仅在文件具有父文件(例如,通过采用父文件的文件构造器之一创建)时才有效。当getParentFile()为null时,您需要求助于使用lastIndexOf,或使用类似Apache Commons的FileNameUtils.getFullPath():

FilenameUtils.getFullPathNoEndSeparator(file.getAbsolutePath());
=> C:/aaa/bbb/ccc/ddd

There are several variants to retain/drop the prefix and trailing separator. You can either use the same FilenameUtils class to grab the name from the result, use lastIndexOf, etc.

保留/删除前缀和尾随分隔符有几种变体。您可以使用相同的FilenameUtils类从结果中获取名称,使用lastIndexOf等。

#2


17  

File f = new File("C:/aaa/bbb/ccc/ddd/test.java");
System.out.println(f.getParentFile().getName())

f.getParentFile() can be null, so you should check it.

f.getParentFile()可以为null,因此您应该检查它。

#3


12  

Use below,

使用下面,

File file = new File("file/path");
String parentPath = file.getAbsoluteFile().getParent();

#4


4  

If you have just String path and don't want to create new File object you can use something like:

如果您只有String路径并且不想创建新的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));
}

#5


2  

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"

if you need to append folder "ddd" by another path use;

如果你需要通过另一个路径使用附加文件夹“ddd”;

String currentFolder= "/" + currentPath.getName().toString();

#6


1  

In Java 7 you have the new Paths api. The modern and cleanest solution is:

在Java 7中,您有了新的Paths api。现代最干净的解决方案是:

Paths.get("C:/aaa/bbb/ccc/ddd/test.java").getParent().getFileName();

#7


0  

From java 7 I would prefer to use Path. You only need to put path into:

从java 7我更喜欢使用Path。你只需要把路径放入:

Path dddDirectoryPath = Paths.get("C:/aaa/bbb/ccc/ddd/test.java");

and create some get method:

并创建一些get方法:

public String getLastDirectoryName(Path directoryPath) {
   int nameCount = directoryPath.getNameCount();
   return directoryPath.getName(nameCount - 1);
}

#8


0  

In Groovy:

在Groovy中:

There is no need to create a File instance to parse the string in groovy. It can be done as follows:

无需创建File实例来解析groovy中的字符串。它可以如下完成:

String path = "C:/aaa/bbb/ccc/ddd/test.java"
path.split('/')[-2]  // this will return ddd

The split will create the array [C:, aaa, bbb, ccc, ddd, test.java] and index -2 will point to entry before the last one, which in this case is ddd

拆分将创建数组[C:,aaa,bbb,ccc,ddd,test.java],索引-2将指向最后一个之前的条目,在本例中为ddd

#1


107  

Use File's getParentFile() method and String.lastIndexOf() to retrieve just the immediate parent directory.

使用File的getParentFile()方法和String.lastIndexOf()仅检索直接父目录。

Mark's comment is a better solution thanlastIndexOf():

Mark的评论比popIndexOf()更好:

file.getParentFile().getName();

These solutions only works if the file has a parent file (e.g., created via one of the file constructors taking a parent File). When getParentFile() is null you'll need to resort to using lastIndexOf, or use something like Apache Commons' FileNameUtils.getFullPath():

这些解决方案仅在文件具有父文件(例如,通过采用父文件的文件构造器之一创建)时才有效。当getParentFile()为null时,您需要求助于使用lastIndexOf,或使用类似Apache Commons的FileNameUtils.getFullPath():

FilenameUtils.getFullPathNoEndSeparator(file.getAbsolutePath());
=> C:/aaa/bbb/ccc/ddd

There are several variants to retain/drop the prefix and trailing separator. You can either use the same FilenameUtils class to grab the name from the result, use lastIndexOf, etc.

保留/删除前缀和尾随分隔符有几种变体。您可以使用相同的FilenameUtils类从结果中获取名称,使用lastIndexOf等。

#2


17  

File f = new File("C:/aaa/bbb/ccc/ddd/test.java");
System.out.println(f.getParentFile().getName())

f.getParentFile() can be null, so you should check it.

f.getParentFile()可以为null,因此您应该检查它。

#3


12  

Use below,

使用下面,

File file = new File("file/path");
String parentPath = file.getAbsoluteFile().getParent();

#4


4  

If you have just String path and don't want to create new File object you can use something like:

如果您只有String路径并且不想创建新的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));
}

#5


2  

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"

if you need to append folder "ddd" by another path use;

如果你需要通过另一个路径使用附加文件夹“ddd”;

String currentFolder= "/" + currentPath.getName().toString();

#6


1  

In Java 7 you have the new Paths api. The modern and cleanest solution is:

在Java 7中,您有了新的Paths api。现代最干净的解决方案是:

Paths.get("C:/aaa/bbb/ccc/ddd/test.java").getParent().getFileName();

#7


0  

From java 7 I would prefer to use Path. You only need to put path into:

从java 7我更喜欢使用Path。你只需要把路径放入:

Path dddDirectoryPath = Paths.get("C:/aaa/bbb/ccc/ddd/test.java");

and create some get method:

并创建一些get方法:

public String getLastDirectoryName(Path directoryPath) {
   int nameCount = directoryPath.getNameCount();
   return directoryPath.getName(nameCount - 1);
}

#8


0  

In Groovy:

在Groovy中:

There is no need to create a File instance to parse the string in groovy. It can be done as follows:

无需创建File实例来解析groovy中的字符串。它可以如下完成:

String path = "C:/aaa/bbb/ccc/ddd/test.java"
path.split('/')[-2]  // this will return ddd

The split will create the array [C:, aaa, bbb, ccc, ddd, test.java] and index -2 will point to entry before the last one, which in this case is ddd

拆分将创建数组[C:,aaa,bbb,ccc,ddd,test.java],索引-2将指向最后一个之前的条目,在本例中为ddd