<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編碼內(nèi)
                            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讀取文件的時候,經(jīng)常碰到中文亂碼,特研究了一下java 的編碼格式,在java 中
    java編碼與txt編碼對應(yīng)
    java txt
    unicode unicode big endian
    utf-8 utf-8
    utf-16 unicode
    gb2312 ANSI
    java 讀取txt如果編碼格式不對就會出現(xiàn)亂碼格式,通過下邊方法獲取文本文件編碼格式,然后以指定的編碼讀取文件,就不會出現(xiàn)亂碼(簡單測試了一下,但是也不保證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) // 單獨出現(xiàn)BF以下的,也算是GBK
    break;
    if (0xC0 <= read && read <= 0xDF) {
    read = bis.read();
    if (0x80 <= read && read <= 0xBF) // 雙字節(jié) (0xC0 - 0xDF)
    // (0x80
    // - 0xBF),也可能在GB編碼內(nèi)
    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)  編輯  收藏

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


    網(wǎng)站導(dǎo)航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    主站蜘蛛池模板: 国产偷国产偷亚洲高清在线| 8x8x华人永久免费视频| 老司机亚洲精品影视www| 国产成人无码区免费内射一片色欲 | 色噜噜综合亚洲av中文无码| 最近2019中文字幕免费看最新| 特黄aa级毛片免费视频播放| 亚洲男人的天堂在线播放| 日韩免费电影在线观看| 免费精品一区二区三区第35| 亚洲欧美不卡高清在线| 亚洲国产精彩中文乱码AV| 日韩一区二区免费视频| 免费一级不卡毛片| 亚洲国产AV无码一区二区三区| 亚洲国产精品免费视频| 国产精品免费视频一区| 最近在线2018视频免费观看| 一个人看的免费观看日本视频www| 亚洲国产精品乱码在线观看97| 亚洲综合国产精品第一页| 特级做A爰片毛片免费69| 成人无码区免费A∨直播| 亚洲成av人片在线天堂无| 亚洲日本精品一区二区| 亚洲日本中文字幕一区二区三区| 性做久久久久久久免费看| av永久免费网站在线观看| WWW国产亚洲精品久久麻豆| 亚洲性无码av在线| 亚洲s色大片在线观看| 亚洲伊人久久成综合人影院| 成年女人毛片免费播放人| 最近2019中文字幕免费直播| 福利免费在线观看| 国产午夜亚洲精品不卡| 亚洲色无码国产精品网站可下载| 亚洲国产精品张柏芝在线观看| 亚洲AV无码第一区二区三区| 亚洲乳大丰满中文字幕| 亚洲精品无码99在线观看 |