`
nishijia
  • 浏览: 53688 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

XMLWriter 之 java.io.FileNotFoundException 问题

    博客分类:
  • JAVA
阅读更多

使用DOM4J 的 XMLWriter 会遇到 java.io.FileNotFoundException 系统找不到指定的路径

 

看下面的代码:

		XMLWriter writer = null;
		try {
		
			OutputFormat format = OutputFormat.createPrettyPrint();// 定义文档的格式
			format.setEncoding("UTF-8");
			writer = new XMLWriter(new FileWriter(path), format);
			writer.write(doc);
			writer.close();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	} 

 这个是因为 path 的 路径 要是包含文件夹的 必须要事先存在的 否则就会报这个错误

 

所以在之前 加上 

File f= new File(Constant.DOM4J_DIR);
			if(!f.exists()){
				logger.debug("create dom4j directionary ");
				f.mkdir();
			}

 就可以了

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics