Java 获取文件父文件夹的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19030545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the folder name of a files parent
提问by TheHamstring
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.
我正在处理一些代码,我希望它根据文件所在的文件夹名称而有不同的行为。我不需要绝对路径只是最终文件夹。到目前为止,我所看到的一切都是使用文件中指定的绝对路径。
采纳答案by James Dunn
This is what you want:
这就是你想要的:
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.
不幸的是,没有预先提供的方法只返回文件路径中最后一个文件夹的名称,因此您必须进行一些字符串操作才能获得它。
回答by SeniorJD
Try java.io.File.getParentFile()method.
尝试java.io.File.getParentFile()方法。
String getFileParentName(File file) {
if (file != null && file.getParentFile() != null) {
return file.getParentFile().getName();
}
return null; // no parent for file
}
回答by shieldgenerator7
There's
有
String File.getParent()
There's also
还有
File File.getParentFile()
I don't know what the return in terms of being absolute or relative, but if it's absolute you can always find the last (or second to last, depending) instance of the "\" character (remember to escape it like this "\\") to denote where the lowest folder level is.
我不知道绝对或相对的回报是什么,但如果它是绝对的,你总是可以找到“\”字符的最后一个(或倒数第二个,取决于)实例(记住像这样转义它“\ \") 表示最低文件夹级别的位置。
For example, if the function returned:
例如,如果函数返回:
"C:\Users\YourName" is where you'd get the last occurance of "\", and all characters after it would be the folder you want
"C:\Users\YourName" 是你最后一次出现 "\" 的地方,它后面的所有字符都是你想要的文件夹
"C:\Users\YourName\" is where you'd get the second to last occurance of "\", and all characters between that and the last "\" would be the folder you're looking for.
“C:\Users\YourName\” 是您倒数第二次出现“\”的地方,并且该位置和最后一个“\”之间的所有字符都是您要查找的文件夹。
Java File API: http://docs.oracle.com/javase/7/docs/api/java/io/File.html
Java 文件 API:http: //docs.oracle.com/javase/7/docs/api/java/io/File.html
回答by Jost
I think java.io.File.getParent()is what you are looking for:
我认为java.io.File.getParent()是你要找的:
import java.io.File;
public class Demo {
public static void main(String[] args) {
File f = null;
String parent="not found";
f = new File("/tmp/test.txt");
parent = f.getParent();
System.out.print("parent name: "+v);
}
}
回答by Ankit Rustagi
String path = "/abc/def"; // path to the directory
try
{
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles)
{
if(file.isDirectory())
{
switch(file.getName)
{
case "folder1" : //do something
break
case "folder2" : //do something else
break
}
}
}
}
catch(Exception e)
{
System.out.println("Directory not Found");
}