トップ 差分 一覧 ping ソース 検索 ヘルプ PDF RSS ログイン

Java 文字コード判定



目次



記事一覧

キーワード

Java 文字コード判定

[Java]<<cateogryプラグインは存在しません。>>


public static String detectEncoding(String text) {
    String encoding = null;

    try {
        UniversalDetector detector = new UniversalDetector(null);

        InputStream is = new StringInputStream(text);
        
        byte[] buf = new byte[1024];
        int nread;
        while ((nread = is.read(buf)) > 0 && !detector.isDone()) {
            detector.handleData(buf, 0, nread);
        }
        detector.dataEnd();
        encoding = detector.getDetectedCharset();
        
        detector.reset();
        
    } catch(Exception e) {
        e.printStackTrace();
    }
    
    return encoding;
}



YAGI Hiroto (piroto@a-net.email.ne.jp)
twitter http://twitter.com/pppiroto

Copyright© 矢木 浩人 All Rights Reserved.