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)注明來(lái)源 http://www.aygfsteel.com/zhyiwww   
          |----------------------------------------------------------------------------------------|
          posted on 2009-06-18 17:15 zhyiwww 閱讀(846) 評(píng)論(0)  編輯  收藏 所屬分類: j2ee
          主站蜘蛛池模板: 宾川县| 康平县| 竹溪县| 互助| 札达县| 千阳县| 苏州市| 富川| 德庆县| 贵港市| 体育| 安远县| 虞城县| 遂川县| 田林县| 丽水市| 孟津县| 九台市| 香港| 中超| 石河子市| 永寿县| 神农架林区| 邵武市| 宁蒗| 沅陵县| 盐池县| 许昌市| 白河县| 黑水县| 共和县| 桐城市| 鹤壁市| 隆子县| 肥东县| 浙江省| 德安县| 博爱县| 龙岩市| 太仆寺旗| 铅山县|