zhyiwww
          用平實的筆,記錄編程路上的點點滴滴………
          posts - 536,comments - 394,trackbacks - 0
          輸入流的編碼
          比如,在上傳文件的時候,我們知道文件的編碼是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.aygfsteel.com/zhyiwww   
          |----------------------------------------------------------------------------------------|
          posted on 2009-06-18 17:15 zhyiwww 閱讀(846) 評論(0)  編輯  收藏 所屬分類: j2ee
          主站蜘蛛池模板: 盐边县| 佳木斯市| 莱西市| 剑川县| 溧水县| 通州市| 临清市| 页游| 大连市| 安庆市| 炉霍县| 吉木萨尔县| 呼和浩特市| 永和县| 惠东县| 黎川县| 郧西县| 始兴县| 开江县| 黄石市| 咸丰县| 祁门县| 英吉沙县| 尼玛县| 樟树市| 革吉县| 金山区| 措美县| 乌海市| 龙泉市| 改则县| 大化| 武平县| 汶川县| 琼结县| 施秉县| 安国市| 望都县| 乌鲁木齐县| 黄大仙区| 辽中县|