FileOutputStream filenotfound的错误
来源:互联网 发布:英文seo外链资源 编辑:程序博客网 时间:2024/05/22 14:04
FileOutputStream 若制定的目录不存在,会报错,说filenotfound
可以这样:
String wordPath="/upload/DutyBatch/";<span style="white-space:pre"></span>String FileNameString="顺义区律师值班表.xls";<span style="white-space:pre"></span>String pathString=request.getSession().getServletContext().getRealPath(wordPath);<span style="white-space:pre"></span>File fileDir =new File(pathString); <span style="white-space:pre"></span> //如果文件夹不存在则创建 <span style="white-space:pre"></span> if (!fileDir .exists() && !fileDir .isDirectory()) <span style="white-space:pre"></span> { <span style="white-space:pre"></span> <span style="white-space:pre"></span>fileDir .mkdir(); <span style="white-space:pre"></span> } <span style="white-space:pre"></span> File file = new File(pathString+"\\"+FileNameString);<span style="white-space:pre"></span> if(!file.exists()) <span style="white-space:pre"></span> { <span style="white-space:pre"></span> try { <span style="white-space:pre"></span> file.createNewFile(); <span style="white-space:pre"></span> } catch (IOException e) { <span style="white-space:pre"></span> // TODO Auto-generated catch block <span style="white-space:pre"></span> e.printStackTrace(); <span style="white-space:pre"></span> } <span style="white-space:pre"></span> } <span style="white-space:pre"></span>out = new FileOutputStream(pathString+"\\"+FileNameString);
做一个判断,文件和文件夹是否存在,不存在就创建。
new File(pathString+"\\"+FileNameString);要写到具体的文件,不是某个文件夹,这样也不会出现某文件拒绝访问的现象。
0 0
- FileOutputStream filenotfound的错误
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- FileInputStream/FileOutputStream的应用
- XWiki安装(war包方式)
- poj1422(二分图,最小路径覆盖)
- Java常见几种排序方式
- Netcat使用方法
- jersey服务端简单小示例
- FileOutputStream filenotfound的错误
- java中的Timestamp
- li浮动引起ul高度坍陷的解决方法
- 蚕茧表示法
- 第一个个人J2EE网站——甜品屋
- android java资源包下载网站
- iOS基础UI瀑布流界面简单搭建
- Android 中常见的小知识总结
- Activiti 工作流的三个小问题