用目录创建一个Java文件 [英] Create a java file with directory

查看:55
本文介绍了用目录创建一个Java文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建文件(/data/test/userid/feedid/test.flv)

I am trying to create a file (/data/test/userid/feedid/test.flv)

如果该目录不存在,则会出现此异常:

If that directory does not exist, I get this exception:

java.io.FileNotFoundException

有什么好办法解决这个问题吗?

Is there any good way to solve this problem?

我已经找到commons.io,但是没有任何函数可以解决这个问题.

I have found commons.io, but there isn't any function that can solve this.

推荐答案

File#mkdirs will create the paths structure denoted by this File. For example

File file = new File("/data/test/userid/feedid/test.flv");
File parent = file.getParentFile();
if (parent.exists() || parent.mkdirs()) {
   //...
} else {
    throw new IOException("Failed to create output directory " + parent);
}

这篇关于用目录创建一个Java文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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