输入流的编码
比如,在上传文件的时候,我们知道文件的编码是GB2312
String fileEnc="GB2312";
byte[] data = ff.getFileData();
InputStream in = new ByteArrayInputStream(data);
// very very important
InputStreamReader isr = new InputStreamReader(in,fileEnc);
BufferedReader br = new BufferedReader(isr);
这样就可以取得一个GB2312的文件输入流
所以,默认的输出是UTF8,比如
FileWriter w = new FileWriter(f);
log.info("the new file encoding is : "+w.getEncoding());
while ((str = br.readLine()) != null) {
w.write(str + "\n");
log.info("line content : " + str);
lines.put(str);
//TODO
// lines.put(encodeString(str, fileEnc));
}
那么输出的文件格式UTF-8
如果你想输出GB2312编码的文件,可以用下面的方法
FileOutputStream out = new FileOutputStream(f);
OutputStreamWriter www = new OutputStreamWriter(out,"GB2312");
log.info("the new file encoding is : "+w.getEncoding());
while ((str = br.readLine()) != null) {
www.write(str + "\n");
log.info("line content : " + str);
lines.put(str);
//TODO
// lines.put(encodeString(str, fileEnc));
}
|----------------------------------------------------------------------------------------|
版权声明 版权所有 @zhyiwww
引用请注明来源 http://www.blogjava.net/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2009-06-18 17:15
zhyiwww 阅读(835)
评论(0) 编辑 收藏 所属分类:
j2ee