博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IO 流读取文件时候出现乱码 文件编码格式问题 怎么转换解决方法
阅读量:4040 次
发布时间:2019-05-24

本文共 2094 字,大约阅读时间需要 6 分钟。

在使用下面这个写法时候UTF-8文件编码 在读取时候出现乱码问题。

File myFile=new File("文件路径");

Java代码  
  1. BufferedReader in = new BufferedReader(new FileReader(myFile));  

应该修改为:

Java代码  
  1. BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream(myFile), "UTF-8") );  
如果使用INSA编码时候 请使用下面文件读取方式:
Java代码  
  1. InputStreamReader reader = new InputStreamReader(   new FileInputStream(new File("文件路径")), "gb2312");  
下面是我对文件编码的判断方法:
Java代码  
  1. /** 
  2.      * 上传文件编码判断 
  3.      * */  
  4.     public static String get_charset(File file) {  
  5.         String charset = "GBK";  
  6.         byte[] first3Bytes = new byte[3];  
  7.         try {  
  8.             boolean checked = false;  
  9.             ;  
  10.             BufferedInputStream bis = new BufferedInputStream(  
  11.                     new FileInputStream(file));  
  12.             bis.mark(0);  
  13.             int read = bis.read(first3Bytes, 03);  
  14.             if (read == -1)  
  15.                 return charset;  
  16.             if (first3Bytes[0] == (byte0xFF && first3Bytes[1] == (byte0xFE) {  
  17.                 charset = "UTF-16LE";  
  18.                 checked = true;  
  19.             } else if (first3Bytes[0] == (byte0xFE  
  20.                     && first3Bytes[1] == (byte0xFF) {  
  21.                 charset = "UTF-16BE";  
  22.                 checked = true;  
  23.             } else if (first3Bytes[0] == (byte0xEF  
  24.                     && first3Bytes[1] == (byte0xBB  
  25.                     && first3Bytes[2] == (byte0xBF) {  
  26.                 charset = "UTF-8";  
  27.                 checked = true;  
  28.             }  
  29.             bis.reset();  
  30.             if (!checked) {  
  31.                 // int len = 0;  
  32.                 int loc = 0;  
  33.   
  34.                 while ((read = bis.read()) != -1) {  
  35.                     loc++;  
  36.                     if (read >= 0xF0)  
  37.                         break;  
  38.                     if (0x80 <= read && read <= 0xBF// 单独出现BF以下的,也算是GBK  
  39.                         break;  
  40.                     if (0xC0 <= read && read <= 0xDF) {  
  41.                         read = bis.read();  
  42.                         if (0x80 <= read && read <= 0xBF// 双字节 (0xC0 - 0xDF)  
  43.                             // (0x80  
  44.                             // - 0xBF),也可能在GB编码内  
  45.                             continue;  
  46.                         else  
  47.                             break;  
  48.                     } else if (0xE0 <= read && read <= 0xEF) {
    // 也有可能出错,但是几率较小  
  49.                         read = bis.read();  
  50.                         if (0x80 <= read && read <= 0xBF) {  
  51.                             read = bis.read();  
  52.                             if (0x80 <= read && read <= 0xBF) {  
  53.                                 charset = "UTF-8";  
  54.                                 break;  
  55.                             } else  
  56.                                 break;  
  57.                         } else  
  58.                             break;  
  59.                     }  
  60.                 }  
  61.   
  62.             }  
  63.   
  64.             bis.close();  
  65.         } catch (Exception e) {  
  66.             e.printStackTrace();  
  67.         }  
  68.   
  69.         return charset;  
  70.     }  
调用时候判断编码方式UTF-8 或是 INSA编码:
Java代码  
  1. BufferedReader br = null;  
  2.             if (charset == "GBK") {  
  3.                 InputStreamReader reader = new InputStreamReader(  
  4.                         new FileInputStream(new File(filepath)), "gb2312");  
  5.                 br = new BufferedReader(reader);  
  6.             }  
  7.             if (charset == "UTF-8") {  
  8.                 br = new BufferedReader(new InputStreamReader(  
  9.                         new FileInputStream(filepath), "UTF-8"));  
  10.             }  

转载地址:http://fcpdi.baihongyu.com/

你可能感兴趣的文章
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
补充自动屏蔽攻击ip
查看>>
谷歌走了
查看>>