码上敲享录 > java入门知识分享 > Java 生成文件,所在文件夹不存在时, 创建文件夹

Java 生成文件,所在文件夹不存在时, 创建文件夹

上一章章节目录下一章 2019-12-02已有2590人阅读 评论(0)

String strPath = "E:\\path\\myfile.txt";

File file = new File(strPath);

if(!file.exists())){

   file.createNewFile();

}

这段代码,如果E:\\path文件夹不存在,会报错。


String strPath = "E:\\path\\myfile.txt";

File file = new File(strPath);

if(!file.exists())){

  file.file.mkdirs();

}

这段代码,会创建文件夹 E:\\path\\myfile.txt。


String strPath = "E:\\path\\myfile.txt";

File file = new File(strPath);

File fileParent = file.getParentFile();

if(!fileParent.exists()){

fileParent.mkdirs();

}

file.createNewFile();

这段代码可以成功创建文件。



向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交