Java 在給定路徑上創建文件,所在文件夾不存在時,如何正確創建。


/**
* 創建多級目錄文件
*
* @param path 文件路徑
* @throws IOException
*/
private void createFile(String path) throws IOException {
if (StringUtils.isNotEmpty(path)) {
File file = new File(path);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
}
        </div>

第二種方法代碼如下:

//路徑
String path = "/usr/sunny/images/product/img/";
File file = new File(path);
//如果路徑不存在,新建
if(!file.exists()&&!file.isDirectory()) {
    file.mkdirs();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM