xhy0422 博客里 http://xhy0422.iteye.com/blog/50235
dom4j写xml文件,写gbk没问题,但写utf-8的乱码,把FileWriter改用FileOutputStream就可以了.
xhy0422 原帖
[code]
try{
/** 将document中的内容写入文件中 */
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
writer.write(document);
writer.close();
/** 执行成功,需返回1 */
returnValue = 1;
}catch(Exception ex){
ex.printStackTrace();
}
[/code]
改后
[code]
XMLWriter writer;
try {
OutputFormat format = OutputFormat.createPrettyPrint();
//format.setEncoding("UTF-8");
FileOutputStream fos = new FileOutputStream(xmlFile);
//writer = new XMLWriter(new FileWriter(xmlFile), format);
writer = new XMLWriter(fos, format);
writer.write(document);
writer.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
logger.error("修改xml文件失败!");
e.printStackTrace();
}
[/code]
dom4j写xml文件,写gbk没问题,但写utf-8的乱码,把FileWriter改用FileOutputStream就可以了.
xhy0422 原帖
[code]
try{
/** 将document中的内容写入文件中 */
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
writer.write(document);
writer.close();
/** 执行成功,需返回1 */
returnValue = 1;
}catch(Exception ex){
ex.printStackTrace();
}
[/code]
改后
[code]
XMLWriter writer;
try {
OutputFormat format = OutputFormat.createPrettyPrint();
//format.setEncoding("UTF-8");
FileOutputStream fos = new FileOutputStream(xmlFile);
//writer = new XMLWriter(new FileWriter(xmlFile), format);
writer = new XMLWriter(fos, format);
writer.write(document);
writer.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
logger.error("修改xml文件失败!");
e.printStackTrace();
}
[/code]