輸入流的編碼
比如,在上傳文件的時候,我們知道文件的編碼是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://m.tkk7.com/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2009-06-18 17:15
zhyiwww 閱讀(841)
評論(0) 編輯 收藏 所屬分類:
j2ee