String strPath = "E:\\a\\aa\\aaa.txt";
File file = new File(strPath);
if(!file.exists())){
    file.createNewFile();
}

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

String strPath = "E:\\a\\aa\\aaa.txt";
File file = new File(strPath);
if(!file.exists())){
    file.file.mkdirs();
}

这段代码,会创建文件夹 E:\\a\\aa\\aaa.txt\。

String strPath = "E:\\a\\aa\\aaa.txt";
File file = new File(strPath);
File fileParent = file.getParentFile();
if(!fileParent.exists()){
    fileParent.mkdirs();
}
file.createNewFile();

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

相关文章:

  • 2021-07-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-20
  • 2021-09-22
  • 2021-06-03
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案