Java 如何使用 File 对象获取文件的目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3657157/
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
How do I get a file's directory using the File object?
提问by Koerr
Consider the code:
考虑代码:
File file = new File("c:\temp\java\testfile");
testfileis a file, and it may or may not exist.
I want to get the directory c:\\temp\\java\\using the Fileobject. How do I go about doing this?
testfile是一个文件,它可能存在也可能不存在。我想c:\\temp\\java\\使用该File对象获取目录。我该怎么做?
采纳答案by Jon Skeet
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 Filedoesexist and isa directory, then exists()and isDirectory()are what you're after.
此外,如果您想确定原始文件File是否存在并且是一个目录,那么exists()和isDirectory()就是您所追求的。
回答by Joel
File.getParent()from Java Documentation
Java 文档中的File.getParent()
回答by YoK
File API File.getParentor File.getParentFileshould 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.isDirectoryAPI
您还可以使用File.isDirectoryAPI检查您的父文件是目录
if(file.isDirectory()){
System.out.println("file is directory ");
}
回答by Jigar Joshi
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()); }
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()); }
回答by Lovenish Goyal
File filePath=new File("your_file_path");
String dir="";
if (filePath.isDirectory())
{
dir=filePath.getAbsolutePath();
}
else
{
dir=filePath.getAbsolutePath().replaceAll(filePath.getName(), "");
}
回答by Saurabh
You can use this
你可以用这个
File dir=new File(TestMain.class.getClassLoader().getResource("filename").getPath());
回答by C.Ikongo
I found this more useful for getting the absolute file location.
我发现这对于获取绝对文件位置更有用。
File file = new File("\TestHello\test.txt");
System.out.println(file.getAbsoluteFile());
回答by Ponaguynik
If you do something like this:
如果你做这样的事情:
File file = new File("test.txt");
String parent = file.getParent();
parentwill be null.
parent将为空。
So to get directory of this file you can do next:
因此,要获取此文件的目录,您可以执行以下操作:
parent = file.getAbsoluteFile().getParent();
回答by nobes
String parentPath = f.getPath().substring(0, f.getPath().length() - f.getName().length());
This would be my solution
这将是我的解决方案

