考虑代码:
File file = new File("c:\\temp\\java\\testfile");
testfile
是一个文件,它可能存在也可能不存在。
我想使用 File
对象获取目录 c:\\temp\\java\\
。我该怎么做?
最佳答案
无论哪种情况,我都希望 file.getParent()
(或 file.getParentFile()
)给你你想要的。
另外,如果你想知道原来的File
does是否存在并且is是一个目录,那么exists()
和 isDirectory()
是你所追求的。
关于java - 如何使用 File 对象获取文件的目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3657157/