<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 495,comments - 227,trackbacks - 0
    http://blog.163.com/wf_shunqiziran/blog/static/176307209201258102217810/

    private String getFilecharset(File sourceFile) {
            
    byte[] first3Bytes = new byte[3];
            
    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile))) {
                bis.mark(
    0);
                
    int read = bis.read(first3Bytes, 03);
                
    if (read == -1) {
                    
    return "GBK"// 文件編碼為 ANSI
                }
                
                
    if (first3Bytes[0== (byte0xFF && first3Bytes[1== (byte0xFE) {
                    
    return "UTF-16LE"// 文件編碼為 Unicode
                }
                
                
    if (first3Bytes[0== (byte0xFE && first3Bytes[1== (byte0xFF) {
                    
    return "UTF-16BE"// 文件編碼為 Unicode big endian
                }
                
                
    if (first3Bytes[0== (byte0xEF && first3Bytes[1== (byte0xBB && first3Bytes[2== (byte0xBF) {
                    
    return "UTF-8"// 文件編碼為 UTF-8
                }
                
                bis.reset();
                
                
    while ((read = bis.read()) != -1) {
                    
    if (read >= 0xF0) {
                        
    break;
                    }
                    
    if (0x80 <= read && read <= 0xBF) {
                        
    break;
                    }
                    
    if (0xC0 <= read && read <= 0xDF) {
                        read 
    = bis.read();
                        
    if (0x80 <= read && read <= 0xBF) {
                            
    // (0x80 - 0xBF),也可能在GB編碼內
                            continue;
                        }
                        
                        
    break;
                    } 
    else if (0xE0 <= read && read <= 0xEF) {// 也有可能出錯,但是幾率較小
                        read = bis.read();
                        
    if (0x80 <= read && read <= 0xBF) {
                            read 
    = bis.read();
                            
    if (0x80 <= read && read <= 0xBF) {
                                
    return "UTF-8";
                            }
                            
    break;
                        }
                        
    break;
                    }
                }
            } 
    catch (Exception e) {
                e.printStackTrace();
            }
            
    return "GBK";
        }





    最近java讀取文件的時候,經常碰到中文亂碼,特研究了一下java 的編碼格式,在java 中
    java編碼與txt編碼對應
    java txt
    unicode unicode big endian
    utf-8 utf-8
    utf-16 unicode
    gb2312 ANSI
    java 讀取txt如果編碼格式不對就會出現亂碼格式,通過下邊方法獲取文本文件編碼格式,然后以指定的編碼讀取文件,就不會出現亂碼(簡單測試了一下,但是也不保證100%)
    private static String getFilecharset(File sourceFile) {
    String charset = "GBK";
    byte[] first3Bytes = new byte[3];
    try {
    boolean checked = false;
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
    bis.mark(0);
    int read = bis.read(first3Bytes, 0, 3);
    if (read == -1) {
    return charset; //文件編碼為 ANSI
    } else if (first3Bytes[0] == (byte) 0xFF
    && first3Bytes[1] == (byte) 0xFE) {
    charset = "UTF-16LE"; //文件編碼為 Unicode
    checked = true;
    } else if (first3Bytes[0] == (byte) 0xFE
    && first3Bytes[1] == (byte) 0xFF) {
    charset = "UTF-16BE"; //文件編碼為 Unicode big endian
    checked = true;
    } else if (first3Bytes[0] == (byte) 0xEF
    && first3Bytes[1] == (byte) 0xBB
    && first3Bytes[2] == (byte) 0xBF) {
    charset = "UTF-8"; //文件編碼為 UTF-8
    checked = true;
    }
    bis.reset();
    if (!checked) {
    int loc = 0;
    while ((read = bis.read()) != -1) {
    loc++;
    if (read >= 0xF0)
    break;
    if (0x80 <= read && read <= 0xBF) // 單獨出現BF以下的,也算是GBK
    break;
    if (0xC0 <= read && read <= 0xDF) {
    read = bis.read();
    if (0x80 <= read && read <= 0xBF) // 雙字節 (0xC0 - 0xDF)
    // (0x80
    // - 0xBF),也可能在GB編碼內
    continue;
    else
    break;
    } else if (0xE0 <= read && read <= 0xEF) {// 也有可能出錯,但是幾率較小
    read = bis.read();
    if (0x80 <= read && read <= 0xBF) {
    read = bis.read();
    if (0x80 <= read && read <= 0xBF) {
    charset = "UTF-8";
    break;
    } else
    break;
    } else
    break;
    }
    }
    }
    bis.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return charset;
    }
    posted on 2015-05-07 15:48 SIMONE 閱讀(1511) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲午夜福利AV一区二区无码| 美女视频黄免费亚洲| 69av免费视频| 免费人成网站7777视频| 亚洲人成网77777色在线播放| 一级做a爰片久久毛片免费陪 | 久久亚洲精品无码网站| 最新亚洲成av人免费看| 国产高清免费视频| 自拍偷自拍亚洲精品第1页| 亚洲免费福利在线视频| 一个人看的免费高清视频日本| 歪歪漫画在线观看官网免费阅读| 国产成人无码综合亚洲日韩| 中文字幕亚洲精品无码| 成人毛片免费观看视频在线| 国产v亚洲v天堂无码网站| a级毛片免费观看视频| 亚洲AV中文无码字幕色三| 免费又黄又爽又猛大片午夜| 最近中文字幕无免费视频| 久久久久久免费一区二区三区 | 色屁屁www影院免费观看视频 | 久久国产成人精品国产成人亚洲| 亚洲乱码日产精品BD在线观看| 男人扒开添女人下部免费视频| 亚洲人成网站18禁止一区| 亚洲欧美成人一区二区三区| 99久久99久久精品免费观看| 免费人成在线观看网站视频| 成人免费观看男女羞羞视频 | 亚洲久热无码av中文字幕| 国产成人无码免费视频97| 亚洲理论在线观看| 黄色视频在线免费观看| 国产精品嫩草影院免费| 一级毛片aaaaaa视频免费看| 国国内清清草原免费视频99| 中文字幕亚洲综合小综合在线| 久久精品国产免费一区| 图图资源网亚洲综合网站|