Method in Java to create a file at a location, creating directories if necessary?

后端 未结 3 1148
日久生厌
日久生厌 2021-02-09 19:31

I am attempting to write a file using java.io, where I am trying to create it at the location \"some/path/to/somewhere/then-my-file\". When the file is being create

相关标签:
3条回答
  • 2021-02-09 20:06

    Guava also has Files.createParentDirs(File).

    0 讨论(0)
  • 2021-02-09 20:13

    Since the question also mentioned the library Apache Common IO, I report in the following a solution that uses this nice library:

    File file = new File("...  the directory path ..."); 
    FileUtils.forceMkdir(file);
    

    This solution uses the class FileUtils, from package org.apache.commons.io and the method forceMkdir, that "Makes a directory, including any necessary but nonexistent parent directories".

    0 讨论(0)
  • 2021-02-09 20:25

    new File("some/path/to/somewhere/then-my-file").getParentFile().mkdirs()

    0 讨论(0)
提交回复
热议问题