使用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();
}
就可以了