Java to create a file on the specified path, when the folder does not exist, how to solve

If the d: \ upload \ file \ folder does not exist, an error

String strPath = "d:\\upload\\file\\2.mp3";
File file = new File(strPath);
if(!file.exists())){
    file.createNewFile();
}

 

 The following will create a folder d: \\ upload \\ file \\ 2.mp3 \

String strPath = "d:\\upload\\file\\2.mp3";
File file = new File(strPath);
if(!file.exists())){
    file.file.mkdirs();
}

 

This code can successfully create a file

String strPath = "d:\\upload\\file\\2.mp3";
File file = new File(strPath);
File fileParent = file.getParentFile();
if(!fileParent.exists()){
    fileParent.mkdirs();
}
file.createNewFile();

 

Guess you like

Origin www.cnblogs.com/QW-lzm/p/12355104.html