The NoteBook of EricKong

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks

 

有一个UTF-8编码的文本文件,用FileReader读取到一个字符串,然后转换字符集:str=new String(str.getBytes(),"UTF-8");结果大部分中文显示正常,但最后仍有部分汉字显示为问号!
Java代码 
public static List<String> getLines(String fileName){  
        List<String> lines=new ArrayList<String>();  
        try {  
            BufferedReader br = new BufferedReader(new FileReader(fileName));  
            String line = null;  
            while ((line = br.readLine()) != null) {  
                lines.add(new String(line.getBytes("GBK"),"UTF-8"));  
            }  
            br.close();  
        } catch (FileNotFoundException e) {  
        }catch (IOException e) {}  
        return lines;  
    } 

public static List<String> getLines(String fileName){
  List<String> lines=new ArrayList<String>();
  try {
   BufferedReader br = new BufferedReader(new FileReader(fileName));
   String line = null;
   while ((line = br.readLine()) != null) {
    lines.add(new String(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代替FileReader,InputStreamReader isr=new InputStreamReader(new FileInputStream(fileName),"UTF-8");这样读取文件就会直接用UTF-8解码,不用再做编码转换。
Java代码 
public static List<String> getLines(String fileName){  
        List<String> lines=new ArrayList<String>();  
        try {  
            BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8"));  
            String line = null;  
            while ((line = br.readLine()) != null) {  
                lines.add(line);  
            }  
            br.close();  
        } catch (FileNotFoundException e) {  
        }catch (IOException e) {}  
        return lines;  
    } 

 

posted on 2012-04-28 21:00 Eric_jiang 阅读(5517) 评论(2)  编辑  收藏 所属分类: Java

Feedback

# re: FileReader读取中文txt文件编码丢失问题(乱码) 2012-04-29 20:12 Eric_jiang
http://code.google.com/p/sqlite-manager/  回复  更多评论
  

# re: FileReader读取中文txt文件编码丢失问题(乱码) 2013-02-20 15:12 网络记事本
FileReader 功能还是不行。
谢谢指点,我已经更换为 FileInputStream 了,这个支持设置编码。  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: