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类上。
若想写入GBK编码的话 就把UTF8换成GBK编码就好了
时间: 2024-10-11 11:05:42