获取文件父文件夹的文件夹名称 [英] Get the folder name of a files parent

查看:938
本文介绍了获取文件父文件夹的文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些代码,我希望它的行为有所不同,具体取决于文件所在的文件夹名称。我不需要绝对路径只是最终文件夹。到目前为止我所看到的一切都是使用文件中指定的绝对路径。

I'm working with some code and I want it to behave differently depending on the folder name that the file is in. I don't need the absolute path just the final folder. Everything that I have seen so far is using a absolute path that is specified in the file.

推荐答案

这就是你想要的:

public static String getParentName(File file) {
    if(file == null || file.isDirectory()) {
            return null;
    }
    String parent = file.getParent();
    parent = parent.substring(parent.lastIndexOf("\\") + 1, parent.length());
    return parent;      
}

不幸的是,没有预先提供的方法只返回名称文件路径中的最后一个文件夹,所以你必须进行一些字符串操作才能得到它。

Unfortunately there is no pre-provided method that just returns the name of the last folder in the file's path, so you have to do some String manipulation to get it.

这篇关于获取文件父文件夹的文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆