如何与缺少的父目录一起创建新文件? [英] How to create a new file together with missing parent directories?

查看:295
本文介绍了如何与缺少的父目录一起创建新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用时

file.createNewFile();

我收到以下异常

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

我想知道是否有创建缺失父目录的createNewFile?

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

推荐答案

你试过这个吗?

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.

这篇关于如何与缺少的父目录一起创建新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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