如何使用File对象获取文件的目录? [英] How do I get a file's directory using the File object?

查看:161
本文介绍了如何使用File对象获取文件的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑代码:

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

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

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?

推荐答案

在任何一种情况下,我都希望 file.getParent() (或 file.getParentFile() )给你你想要的东西。

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

另外,如果你想找出来的话原始文件 是否存在且目录,然后存在() isDirectory()是您追求的目标。

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.

这篇关于如何使用File对象获取文件的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆