简体   繁体   English

获取父文件的文件夹名称

[英]Get the folder name of a files parent

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. 到目前为止,我所看到的所有内容都使用了文件中指定的绝对路径。

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. 不幸的是,没有预先提供的方法仅返回文件路径中最后一个文件夹的名称,因此您必须执行一些String操作才能获取它。

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
}

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);
   }

}

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

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"); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM