zhyiwww
          用平實(shí)的筆,記錄編程路上的點(diǎn)點(diǎn)滴滴………
          posts - 536,comments - 394,trackbacks - 0
          輸入流的編碼
          比如,在上傳文件的時(shí)候,我們知道文件的編碼是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);

          這樣就可以取得一個(gè)GB2312的文件輸入流

          所以,默認(rèn)的輸出是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));
          ?? ??? ??? ?}

          ?? ??? ??? ?




          |----------------------------------------------------------------------------------------|
                                     版權(quán)聲明  版權(quán)所有 @zhyiwww
                      引用請(qǐng)注明來源 http://www.aygfsteel.com/zhyiwww   
          |----------------------------------------------------------------------------------------|
          posted on 2009-06-18 17:15 zhyiwww 閱讀(842) 評(píng)論(0)  編輯  收藏 所屬分類: j2ee
          主站蜘蛛池模板: 阿尔山市| 嘉善县| 和硕县| 个旧市| 永州市| 偏关县| 视频| 怀仁县| 凌海市| 霍林郭勒市| 海兴县| 工布江达县| 改则县| 昆明市| 玉环县| 天镇县| 潮安县| 闻喜县| 大姚县| 巢湖市| 普安县| 达拉特旗| 黔西| 乐清市| 甘泉县| 枣强县| 张家港市| 安福县| 盈江县| 北川| 辛集市| 图们市| 上思县| 仁怀市| 清涧县| 马关县| 扎囊县| 冷水江市| 邵阳县| 青州市| 安庆市|