有一个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取代FileReader,InputStreamReaderisr=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;   

    }

FileReader读取文件里文乱码问题的更多相关文章

  1. HTML5 文件域+FileReader 读取文件(一)

    在HTML5以前,HTML的文件上传域的功能具有很大的局限性,这种局限性主要体现在如下两点: 每次只能选择一个文件进行上传 客户端代码只能获取被上传文件的文件路径,无法访问实际的文件内容 一.File ...

  2. H5 FileReader读取文件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. FileReader读取文件详解

    FileReader是一种异步文件读取机制,结合input:file可以很方便的读取本地文件. input:file 在介绍FileReader之前,先简单介绍input的file类型. <in ...

  4. FileReader读取文件

    前言:FileReader是一种异步文件读取机制,结合input:file可以很方便的读取本地文件. input:file 在介绍FileReader之前,先简单介绍input的file类型. < ...

  5. FileReader()读取文件、图片上传预览

    前言 FileReader 对象允许Web应用程序异步读取存储在用户计算机上的文件(或原始数据缓冲区)的内容,使用 File 或 Blob 对象指定要读取的文件或数据. 其中File对象可以是来自用户 ...

  6. HTML5 文件域+FileReader 读取文件并上传到服务器(三)

    一.读取文件为blob并上传到服务器 HTML <div class="container"> <!--读取要上传的文件--> <input type ...

  7. HTML5 文件域+FileReader 读取文件(二)

    一.读取文本文件内容,指定字符编码 <div class="container"> <!--文本文件验证--> <input type="f ...

  8. 【转】c# winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建,写入,修改

    创建文件和读取文件的值 #region 判断文件是否存在,不存在则创建,否则读取值显示到窗体 public FormMain() { InitializeComponent(); //ReadFile ...

  9. node.js在读取文件时中文乱码问题

    断更很久了........从今天开始会努力的持续更博,积极学习. 言归正传.今天在写node.js的demo时发现一个bug.我在node中读取本地的text文件时,发现英文的内容可以被读取,但是中文 ...

随机推荐

  1. android在view.requestFocus(0)返回false的解决办法

    我们有时候想让listview的第一行自动获取到焦点,我们就会使用view.requestFocus(0)来操作,而有时候并不生效,debug后显示rerurn为false. 这是因为我们获取焦点太早 ...

  2. Web网页中内嵌Activex的Activex插件开发 .

    转载自: http://blog.csdn.net/tttyd/article/details/5258096 源代码下载 http://files.cnblogs.com/tttyd/Activex ...

  3. POJ 3304 Segments(计算几何)

    意甲冠军:给出的一些段的.问:能否找到一条直线,通过所有的行 思维:假设一条直线的存在,所以必须有该过两点的线,然后列举两点,然后推断是否存在与所有的行的交点可以是 代码: #include < ...

  4. 【leetcode】Single Number II

    int singleNumber(int A[], int n) { int once = 0; int twice = 0; int three = 0; for (int i = 0; i < ...

  5. CSS属性总结——思路很清晰

    CSS 属性总结 CSS的属性是用来改变文档元素的状态的,其中主要改变两方面的东西,即位置.样式,现在我们就将CSS的属性分为定位和样式两方面来总结,知识结构图如下:     用来定位的属性: 在同一 ...

  6. 删掉SafeDrv病毒(这个病毒有点意思)

    1.手动删除以下文件: %program files%\common files\safedrv.exe %documents and settings%\administrator\rkoxe.dr ...

  7. ASP.NET - 匹配标签中的内容

    string str = @"<td>Csdn</td>\r\n<td>V1.0</td>\r\n<td>2014-10-23&l ...

  8. Linux路由器

    Linux路由器 武溪嵌人 钻研嵌入式,改变未来 http://blog.csdn.net/zhang_danf     Linux路由器 http://blog.csdn.net/zhang_dan ...

  9. Delphi - 闲来无事,自己写个Timer玩玩(多线程Timer)

    明天去坐火车,回家,今天就没有事做,本来在弄一个跨进程获取其他程序里面组件,如ListView,ListBox,Button等的信息,突然有个想法自己写个Timer,不用SetTimer函数,我们自己 ...

  10. 国内使用Google Maps JavaScript API

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="ini ...