使用Java nio创建子目录和文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21958083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:39:10  来源:igfitidea点击:

Using Java nio to create a subdirectory and file

javafilenio

提问by user3341332

I'm creating a simple program that will try to read in "conf/conf.xml" from disk, but if this file or dir doesn't exist will instead create them.

我正在创建一个简单的程序,它将尝试从磁盘读取“conf/conf.xml”,但如果此文件或目录不存在,则会创建它们。

I can do this using the following code:

我可以使用以下代码执行此操作:

    // create subdirectory path
    Path confDir = Paths.get("./conf"); 

    // create file-in-subdirectory path
    Path confFile = Paths.get("./conf/conf.xml"); 

    // if the sub-directory doesn't exist then create it
    if (Files.notExists(confDir)) { 
        try { Files.createDirectory(confDir); }
        catch (Exception e ) { e.printStackTrace(); }
    }

    // if the file doesn't exist then create it
    if (Files.notExists(confFile)) {
        try { Files.createFile(confFile); }
        catch (Exception e ) { e.printStackTrace(); }
    }

My questions is if this really the most elegant way to do this? It seems superflous to need to create two Paths simple to create a new file in a new subdirectory.

我的问题是这是否真的是最优雅的方式来做到这一点?需要创建两个简单的路径来在新子目录中创建新文件似乎是多余的。

采纳答案by Marko Gre?ak

You could declare your confFileas Fileinstead of Path. Then you can use confFile.getParentFile().mkdirs();, see example below:

你可以声明你的confFileasFile而不是Path. 然后你可以使用confFile.getParentFile().mkdirs();,见下面的例子:

// ...

File confFile = new File("./conf/conf.xml"); 
confFile.getParentFile().mkdirs();

// ...

Or, using your code as is, you can use:

或者,按原样使用您的代码,您可以使用:

Files.createDirectories(confFile.getParent());

回答by user3504158

You could do the following:

您可以执行以下操作:

// Get your Path from the string
Path confFile = Paths.get("./conf/conf.xml"); 
// Get the portion of path that represtents directory structure.  
Path subpath = confFile.subpath(0, confFile.getNameCount() - 1);
// Create all directories recursively
/**
     * Creates a directory by creating all nonexistent parent directories first.
     * Unlike the {@link #createDirectory createDirectory} method, an exception
     * is not thrown if the directory could not be created because it already
     * exists.
     *
*/
Files.createDirectories(subpath.toAbsolutePath()))

回答by Valeriy K.

You can create directory and file in one code line:

您可以在一行代码中创建目录和文件:

Files.createFile(Files.createDirectories(confDir).resolve(confFile.getFileName()))

Files.createFile(Files.createDirectories(confDir).resolve(confFile.getFileName()))

Files.createDirectories(confDir)will not throw an exception if the folder already exists and returns Path in any case.

Files.createDirectories(confDir)如果文件夹已经存在,则不会抛出异常并在任何情况下返回 Path。