createNewFile报错 open failed: ENOENT (No such file or directory)

Keep_Holding_On 2017-03-30 05:32:59
所有你需要做的就是先把文件之前的文件夹创建出来,然后再创建文件,就是这么简单
public static File getFilePath(String filePath,  
String fileName) {
File file = null;
makeRootDirectory(filePath);
try {
file = new File(filePath + fileName);
} catch (Exception e) {
e.printStackTrace();
}
return file;
}

public static void makeRootDirectory(String filePath) {
File file = null;
try {
file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
} catch (Exception e) {

}
}
...全文
641 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
幽饮烛 2017-03-31
  • 打赏
  • 举报
回复
很多情况你没考虑到, 1、mkdir() 只能创建一层目录,也就是说,如果要创建的目录的上级目录不存在,创建不了。 2、权限问题导致无法创建目录。 3、filePath是一个文件,而不是文件夹。 ……
自由自在_Yu 2017-03-31
  • 打赏
  • 举报
回复
weixin_35089616 2017-03-31
  • 打赏
  • 举报
回复
正常来讲,传入一个文件路径,如“/storage/sdcard0/Alice/config/diceconfig.txt”,是会自己创建上级文件夹,及目标文件的,在另外一个项目中完全没问题;但偏偏目前的项目有问题,所以才想了个这办法

50,504

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧