zoukankan      html  css  js  c++  java
  • FileReader读取文件里文乱码问题

    有一个UTF-8编码的文本文件,用FileReader读取到一个字符串,然后转换字符集:str=newString(str.getBytes(),"UTF-8");结果大部分中文显示正常,但最后仍有部分汉字显示为问号!

     
    Java
    代码  
    public static List<String> getLines(String fileName){   
            List<String> lines=newArrayList<String>();   
            try {   
               BufferedReader br = new BufferedReader(new FileReader(fileName));   
                String line= null;   
                while ((line= br.readLine()) != null) {   
                   lines.add(newString(line.getBytes("GBK"),"UTF-8"));   
               }   
               br.close();   
            } catch (FileNotFoundException e){   
            }catch (IOException e){}   
            return lines;   
        } 

    public staticList<String> getLines(String fileName){
      List<String> lines=new ArrayList<String>();
      try {
       BufferedReader br = new BufferedReader(newFileReader(fileName));
       String line = null;
       while ((line = br.readLine()) != null) {
        lines.add(newString(line.getBytes("GBK"),"UTF-8"));
       }
       br.close();
      } catch (FileNotFoundException e) {
      }catch (IOException e) {}
      return lines;
     }

     

    文件读入时是按OS的默认字符集即GBK解码的,我先用默认字符集GBK编码str.getBytes(“GBK”)。此时应该还原为文件里的字节序列了。然后再按UTF-8解码,生成的字符串按理说应该就应该是正确的。

    为什么结果中还是有部分乱码呢? 
    问题出在FileReader读取文件的过程中,FileReader继承了InputStreamReader。但并没有实现父类中带字符集參数的构造函数,所以FileReader仅仅能按系统默认的字符集来解码,然后在UTF-8 -> GBK-> UTF-8的过程中编码出现损失,造成结果不能还原最初的字符。

    原因明白了,这个问题解决起来并不困难。用InputStreamReader取代FileReaderInputStreamReaderisr=new InputStreamReader(new FileInputStream(fileName),"UTF-8");这样读取文件就会直接用UTF-8解码。不用再做编码转换。

     
    Java
    代码  
    public static List<String> getLines(String fileName){   
            List<String> lines=newArrayList<String>();   
            try {   
               BufferedReader br=new BufferedReader(new InputStreamReader(newFileInputStream(fileName),"UTF-8"));   
                String line= null;   
                while ((line= br.readLine()) != null) {   
                   lines.add(line);   
               }   
               br.close();   
            } catch (FileNotFoundException e){   
            }catch (IOException e){}   
            return lines;   
        }  

  • 相关阅读:
    使用Yeoman搭建 AngularJS 应用 (9) —— 让我们搭建一个网页应用
    advance shading——菲涅耳现象
    现代编译原理--第六章(中间树 IR Tree 含源码)
    现代编译原理--第五章(活动记录)
    现代编译原理--第四章(语义分析以及源码)
    现代编译原理--第三章(抽象语法树以及源码)
    现代编译原理--第二章(语法分析之LL(K))
    现代编译原理--第一章(词法分析)
    advance shading--BRDF
    个人作业——软件工程实践总结作业
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5058729.html
Copyright © 2011-2022 走看看