NIO getParentFile()。mkdir() [英] NIO getParentFile().mkdir()

查看:245
本文介绍了NIO getParentFile()。mkdir()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法一次性创建文件和目录
,如下所示...(使用Java 7和NIO ...路径和文件静态方法)。

Is there a way to create a file and directory in one shot as in below... (Using Java 7 and NIO... Paths and Files static methods ).

你不必输入路径,然后在单独的代码行中输入文件?

where you wouldn't have to type the Path and then file in separate lines ( of code ) ?

File file = new File("Library\\test.txt");
if (file.getParentFile().mkdir()) {
    file.createNewFile();
} else {
    throw new IOException("Failed to create directory " + file.getParent());
}

基本上寻找getParentFile()。mkdir()的等效方法关闭在Java 7 NIO中输入的路径(和文件)。

Basically looking for the equivalent approach to "getParentFile().mkdir()" off the Path ( and file ) entered in Java 7 NIO.

Thx

推荐答案

实际上已经意识到这种方式已经完成了..

Actually realized it's accopmplished this way..

Path file = Paths.get("/Users/jokrasa/Documents/workspace_traffic/javaReviewFeb28/src/TEST/","testy.txt");
        try {
            Files.createDirectory(file.getParent());
            Files.createFile(file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

所以你不必实际输入两次......

So you don't have to type it in twice actually...

干杯!

这篇关于NIO getParentFile()。mkdir()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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