如何创建一个新文件以及缺少的父目录?

发布于 2024-09-06 16:46:17 字数 265 浏览 2 评论 0原文

使用时

file.createNewFile();

出现以下异常,

java.io.IOException: Parent directory of file does not exist: /.../pkg/databases/mydb

我想知道是否有一个 createNewFile 可以创建丢失的父目录?

When using

file.createNewFile();

I get the following exception

java.io.IOException: Parent directory of file does not exist: /.../pkg/databases/mydb

I am wondering is there a createNewFile that creates the missing parent directories?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无所谓啦 2024-09-13 16:46:17

你试过这个吗?

file.getParentFile().mkdirs();
file.createNewFile();

我不知道单个方法调用可以做到这一点,但是作为两个语句就非常简单了。

Have you tried this?

file.getParentFile().mkdirs();
file.createNewFile();

I don't know of a single method call that will do this, but it's pretty easy as two statements.

雪若未夕 2024-09-13 16:46:17

从java7开始,您还可以使用NIO2 API:

void createFile() throws IOException {
    Path fp = Paths.get("dir1/dir2/newfile.txt");
    Files.createDirectories(fp.getParent());
    Files.createFile(fp);
}

As of java7, you can also use NIO2 API:

void createFile() throws IOException {
    Path fp = Paths.get("dir1/dir2/newfile.txt");
    Files.createDirectories(fp.getParent());
    Files.createFile(fp);
}
夜清冷一曲。 2024-09-13 16:46:17

如果您确定用于创建文件的路径字符串包含父目录,即如果您确定路径的形式为 /,则乔恩的答案有效。

如果不是,即它是 形式的相对路径,则 getParentFile() 将返回 null

例如

File f = new File("dir/text.txt");
f.getParentFile().mkdirs();     // works fine because the path includes a parent directory.

File f = new File("text.txt");
f.getParentFile().mkdirs();     // throws NullPointerException because the parent file is unknown, i.e. `null`.

,如果您的文件路径可能包含也可能不包含父目录,那么使用以下代码会更安全:

File f = new File(filename);
if (f.getParentFile() != null) {
  f.getParentFile().mkdirs();
}
f.createNewFile();

Jon's answer works if you are certain that the path string with which you are creating a file includes parent directories, i.e. if you are certain that the path is of the form <parent-dir>/<file-name>.

If it does not, i.e. it is a relative path of the form <file-name>, then getParentFile() will return null.

E.g.

File f = new File("dir/text.txt");
f.getParentFile().mkdirs();     // works fine because the path includes a parent directory.

File f = new File("text.txt");
f.getParentFile().mkdirs();     // throws NullPointerException because the parent file is unknown, i.e. `null`.

So if your file path may or may not include parent directories, you are safer with the following code:

File f = new File(filename);
if (f.getParentFile() != null) {
  f.getParentFile().mkdirs();
}
f.createNewFile();
还不是爱你 2024-09-13 16:46:17

在搜索 Kotlin 解决方案时,会弹出这个通用标题的问题。
因此,对于 Kotlin 1.9.0 及更高版本,有一个新的 Path 类上的 createParentDirectories() 方法:

Path("my/path/to/file.txt").createParentDirectories()

Searching for a Kotlin solution this generally-titled question pops up.
So, for Kotlin 1.9.0 and newer there is a new createParentDirectories() method on Path class:

Path("my/path/to/file.txt").createParentDirectories()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文