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

后端 未结 3 1149
日久生厌
日久生厌 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条回答
  •  闹比i
    闹比i (楼主)
    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".

提交回复
热议问题