75年生人,程序员,在西安。

Java FileWriter 输出 UTF-8 的写法(转)

原文:http://www.malcolmhardie.com/weblogs/angus/2004/10/23/java-filewriter-xml-and-utf-8/

Java FileWriter 默认是用(ISO-8859-1 or US-ASCII)西方编码的,总之不是UTF-8的,而FileWriter类有getEncoding方法,却没有setEncoding的方法,如下的写法可以使正确输出UTF-8的文件:

OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path),"UTF-8");

或者

Writer out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(this.outputFilename),”UTF-8″));

使用装饰者模式转到Writer类上。

评论

© 世风十三 | Powered by LOFTER