How to create a new file together with missing parent directories?

后端 未结 3 1836
醉话见心
醉话见心 2020-12-08 09:04

When using

file.createNewFile();

I get the following exception

java.io.IOException: Parent directory of file does not exis         


        
3条回答
  •  孤城傲影
    2020-12-08 09:26

    Have you tried this?

    file.getParentFile().mkdirs();
    file.createNewFile();
    

    I don't know of a single method call that will do this, but it's pretty easy as two statements.

提交回复
热议问题