如何使用File对象获取文件的目录?

时间:2022-09-02 13:53:17

Consider the code:

考虑一下代码:

File file = new File("c:\\temp\\java\\testfile");

testfile is a file, and it may or may not exist. I want to get the directory c:\\temp\\java\\ using the File object. How do I go about doing this?

testfile是一个文件,它可能存在也可能不存在。我想使用File对象获取目录c:\\ temp \\ java \\。我该怎么做呢?

9 个解决方案

#1


130  

In either case, I'd expect file.getParent() (or file.getParentFile()) to give you what you want.

在任何一种情况下,我都希望file.getParent()(或file.getParentFile())能够满足您的需求。

Additionally, if you want to find out whether the original File does exist and is a directory, then exists() and isDirectory() are what you're after.

另外,如果你想知道原始文件是否存在并且是一个目录,那么exists()和isDirectory()就是你所追求的。

#2


21  

File.getParent() from Java Documentation

Java文档中的File.getParent()

#3


7  

File API File.getParent or File.getParentFile should return you Directory of file.

文件API File.getParent或File.getParentFile应该返回文件目录。

Your code should be like :

你的代码应该是这样的:

    File file = new File("c:\\temp\\java\\testfile");
    if(!file.exists()){
        file = file.getParentFile();
    }

You can additionally check your parent file is directory using File.isDirectory API

您还可以使用File.isDirectory API检查您的父文件是否为目录

if(file.isDirectory()){
    System.out.println("file is directory ");
}

#4


4  

File directory = new File("Enter any 
                directory name or file name");
boolean isDirectory = directory.isDirectory();
if (isDirectory) {
  // It returns true if directory is a directory.
  System.out.println("the name you have entered 
         is a directory  : "  +    directory);  
  //It returns the absolutepath of a directory.
  System.out.println("the path is "  + 
              directory.getAbsolutePath());
} else {
  // It returns false if directory is a file.
  System.out.println("the name you have
   entered is a file  : " +   directory);
  //It returns the absolute path of a file.
  System.out.println("the path is "  +  
            file.getParent());
}

#5


2  

File filePath=new File("your_file_path");
String dir="";
if (filePath.isDirectory())
{
    dir=filePath.getAbsolutePath();
}
else
{
    dir=filePath.getAbsolutePath().replaceAll(filePath.getName(), "");
}

#6


2  

If you do something like this:

如果您这样做:

File file = new File("test.txt");
String parent = file.getParent();

parent will be null.

parent将为null。

So to get directory of this file you can do next:

因此,要获取此文件的目录,您可以执行下一步:

parent = file.getAbsoluteFile().getParent();

#7


0  

You can use this

你可以用它

 File dir=new File(TestMain.class.getClassLoader().getResource("filename").getPath());

#8


0  

String parentPath = f.getPath().substring(0, f.getPath().length() - f.getName().length()); 

This would be my solution

这将是我的解决方案

#9


-1  

I found this more useful for getting the absolute file location.

我发现这对于获取绝对文件位置更有用。

File file = new File("\\TestHello\\test.txt");
System.out.println(file.getAbsoluteFile());

#1


130  

In either case, I'd expect file.getParent() (or file.getParentFile()) to give you what you want.

在任何一种情况下,我都希望file.getParent()(或file.getParentFile())能够满足您的需求。

Additionally, if you want to find out whether the original File does exist and is a directory, then exists() and isDirectory() are what you're after.

另外,如果你想知道原始文件是否存在并且是一个目录,那么exists()和isDirectory()就是你所追求的。

#2


21  

File.getParent() from Java Documentation

Java文档中的File.getParent()

#3


7  

File API File.getParent or File.getParentFile should return you Directory of file.

文件API File.getParent或File.getParentFile应该返回文件目录。

Your code should be like :

你的代码应该是这样的:

    File file = new File("c:\\temp\\java\\testfile");
    if(!file.exists()){
        file = file.getParentFile();
    }

You can additionally check your parent file is directory using File.isDirectory API

您还可以使用File.isDirectory API检查您的父文件是否为目录

if(file.isDirectory()){
    System.out.println("file is directory ");
}

#4


4  

File directory = new File("Enter any 
                directory name or file name");
boolean isDirectory = directory.isDirectory();
if (isDirectory) {
  // It returns true if directory is a directory.
  System.out.println("the name you have entered 
         is a directory  : "  +    directory);  
  //It returns the absolutepath of a directory.
  System.out.println("the path is "  + 
              directory.getAbsolutePath());
} else {
  // It returns false if directory is a file.
  System.out.println("the name you have
   entered is a file  : " +   directory);
  //It returns the absolute path of a file.
  System.out.println("the path is "  +  
            file.getParent());
}

#5


2  

File filePath=new File("your_file_path");
String dir="";
if (filePath.isDirectory())
{
    dir=filePath.getAbsolutePath();
}
else
{
    dir=filePath.getAbsolutePath().replaceAll(filePath.getName(), "");
}

#6


2  

If you do something like this:

如果您这样做:

File file = new File("test.txt");
String parent = file.getParent();

parent will be null.

parent将为null。

So to get directory of this file you can do next:

因此,要获取此文件的目录,您可以执行下一步:

parent = file.getAbsoluteFile().getParent();

#7


0  

You can use this

你可以用它

 File dir=new File(TestMain.class.getClassLoader().getResource("filename").getPath());

#8


0  

String parentPath = f.getPath().substring(0, f.getPath().length() - f.getName().length()); 

This would be my solution

这将是我的解决方案

#9


-1  

I found this more useful for getting the absolute file location.

我发现这对于获取绝对文件位置更有用。

File file = new File("\\TestHello\\test.txt");
System.out.println(file.getAbsoluteFile());