FileReader的编码问题
有一个UTF-8编码的文本文件,用FileReader读取到一个字符串,然后转换字符集:str=new String(str.getBytes(),"UTF-8");结果大部分中文显示正常,但最后仍有部分汉字显示为问号!
- 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解码,不用再做编码转换。
- 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;
- }
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;
}
FileReader的编码问题的更多相关文章
- java指定编码的按行读写txt文件(几种读写方式的比较)
转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976 版权声明:本文为博主原创文章,未经博主允许不得转载. ...
- J04-Java IO流总结四 《 FileReader和FileWriter 》
FileReader和FileWriter的源码相对简单,下面通过分析它们的源码以更好地进行理解这两个流 1. FileReader FileReader实现了读取底层的字节数据并将其转换为字符数据的 ...
- Java IO_003.Reader与Writer--字符流以及编码对数据的操作(读取与写入)
Java IO之Reader与Writer对象常用操作(包含了编码问题的处理) 涉及到文件(非文件夹)内容的操作,如果是纯文本的情况下,除了要用到File(见之前文章),另外就必须用到字符输入流或字符 ...
- Java 持久化之 --io流与序列化操作
1)File类操作文件的属性 1.File类的常用方法 1. 文件的绝对完整路径:getAbsolutePath() 文件名:getName() 文件相对路径:getPath() 文件的上一级目录:g ...
- Java 持久化操作之 --io流与序列化
1)File类操作文件的属性 1.File类的常用方法 1. 文件的绝对完整路径:getAbsolutePath() 文件名:getName() 文件相对路径:getPath() 文件的上一级目录:g ...
- I/O流复制文本
package io; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io. ...
- Java日常开发的21个坑,你踩过几个?
前言 最近看了极客时间的<Java业务开发常见错误100例>,再结合平时踩的一些代码坑,写写总结,希望对大家有帮助,感谢阅读~ 1. 六类典型空指针问题 包装类型的空指针问题 级联调用的空 ...
- Java API —— 编码 & IO流( InputStreamReader & OutputStreamWriter & FileReader & FileWriter & BufferedReader & BufferedWriter )
1.编码 1)编码表概述 由字符及其对应的数值组成的一张表 2)常见编码表 · ASCII/Unicode 字符集:ASCII是美国标准信息交换码,用一 ...
- FileReader读取中文txt文件编码丢失问题(乱码)(转)
有一个UTF-8编码的文本文件,用FileReader读取到一个字符串,然后转换字符集:str=new String(str.getBytes(),"UTF-8");结果大部分中文 ...
随机推荐
- 学习笔记:TypeScript入门——基础类型
前言: TypeScript官网断断续续看过几遍,不知道项目中如何使用,有机会还是要实践一下.现在再把文档上不懂的知识点理一遍. 基础类型 1.什么是元组Tuple? 元组类型允许表示一个已知元素数量 ...
- python3 turtle 画国际象棋棋盘
python3 turtle 画国际象棋棋盘 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan import turt ...
- ZOJ 2679 Old Bill ||ZOJ 2952 Find All M^N Please 两题水题
2679:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1679 2952:http://acm.zju.edu.cn/onli ...
- [RxJS] Connection operator: multicast and connect
We have seen how Subjects are useful for sharing an execution of an RxJS observable to multiple obse ...
- 如何把canvas元素作为网站背景总结详解
如何把canvas元素作为网站背景总结详解 一.总结 一句话总结:最简单的做法是绝对定位并且z-index属性设置为负数. 1.如何把canvas元素作为网站背景的两种方法? a.设置层级(本例代码就 ...
- sql海量数据优化
1.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如: select id from t where num is null 可以在num上设 ...
- ZOJ 1494 Climbing Worm 数学水题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=494 题目大意: 一只蜗牛要从爬上n英寸高的地方,他速度为u每分钟,他爬完u需要 ...
- PBOC
http://blog.sina.com.cn/s/blog_64cc82620100rcgu.html 最近在做一个基于PBOC电子现金卡的终端应用, 项目还没有完成, 但电子现金部分的处理模块已完 ...
- VS提示SurfFeatureDetector不是cv的成员函数 .
原因:没有把 opencv_nonfree243d.lib 加入lib库中. 还有两个头文件:#include <opencv2/nonfree/features2d.hpp>#inclu ...
- [Angular] Bind async requests in your Angular template with the async pipe and the "as" keyword
Angular allows us to conveniently use the async pipe to automatically register to RxJS observables a ...