字符编码检测和处理

       由于字符编码经常导致一些异常问题,而且不是很好定位,一直是写健壮性代码的困惑之一。为此我在调研开源的cpdelector等基础上进行封装,实现了文本文件编码检测和字符串的编码检测,以及统一的文本字符处理,方便了程序调用的使用,以供大家参考。

     测试程序如下,改造工程见附件!

UserTextDelector .java

package cpdetector;

import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.ParsingDetector;
import info.monitorenter.cpdetector.io.UnicodeDetector;


import java.io.ByteArrayInputStream;
import java.io.File;
import java.nio.charset.Charset;


public class UserTextDelector {

public String CheckFileCharsetEncode(String strFileLoc)
{

CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();   
detector.add(new ParsingDetector(false));    
detector.add(JChardetFacade.getInstance());   
//ASCIIDetector用于ASCII编码测定   
detector.add(ASCIIDetector.getInstance());   
//UnicodeDetector用于Unicode家族编码的测定   
detector.add(UnicodeDetector.getInstance());   
Charset charset = null;   
File f=new File(strFileLoc);   
try {   
     charset = detector.detectCodepage(f.toURI().toURL());  
} catch (Exception ex) {ex.printStackTrace();}   
if(charset!=null){   
    System.out.println(f.getName()+"编码是:"+charset.name());   
}else  
    System.out.println(f.getName()+"未知"); 

return charset.name();
}

public String CheckContentCharsetEncode(String strContent)
{
CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();   
detector.add(new ParsingDetector(false));    
detector.add(JChardetFacade.getInstance());   
//ASCIIDetector用于ASCII编码测定   
detector.add(ASCIIDetector.getInstance());   
//UnicodeDetector用于Unicode家族编码的测定   
detector.add(UnicodeDetector.getInstance());   

Charset charset = null;   
ByteArrayInputStream bis = new ByteArrayInputStream(strContent.getBytes()); 
try {   
     charset = detector.detectCodepage(bis, 1000);
     bis.close();
} catch (Exception ex) {ex.printStackTrace();}   
if(charset!=null){   
    System.out.println("编码是:"+charset.name());   
}else  
   System.out.println("未知"); 

if(charset.name().equalsIgnoreCase("windows-1252"))
{
return "gb2312";
}
else
{
return charset.name();
}
}
}

GenTextEncode.java

package cpdetector;

public class GenTextEncode {

public String GenTextToGBK(String strContent)
{
UserTextDelector utd = new UserTextDelector();

String strRetGBK = "";
try
{
String strCharsetName = utd.CheckContentCharsetEncode(strContent);

strRetGBK = new String(strContent.getBytes(),strCharsetName); 

strRetGBK = new String(strRetGBK.getBytes("GBK"));

}catch(Exception e)
{
e.printStackTrace();
}

return strRetGBK;
}
}


TestCheckCode .java

import java.io.*;
import java.nio.charset.*;
import info.monitorenter.cpdetector.io.*;
import cpdetector.*;


public class TestCheckCode {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println("Hello world!");

UserTextDelector tcc = new UserTextDelector();//检测类
//tcc.CheckFileCharsetEncode("G://wokspace//java//cpdetector//cpdetector//MPL-1.1.txt");
GenTextEncode gte = new GenTextEncode();//转换类
try
{
String t = "zhonzonggddddddddddd中国ddd";
System.out.println(gte.GenTextToGBK(t));

String utf8 = new String(t.getBytes("UTF-8"));  
System.out.println(gte.GenTextToGBK(utf8));

String iso = new String(t.getBytes("iso-8859-1"));
System.out.println(gte.GenTextToGBK(iso));


String t = "zhonzonggddddddddddd中国ddd";
System.out.println(tcc.CheckContentCharsetEncode(t));
System.out.println(t);

String utf8 = new String(t.getBytes("UTF-8"));  
System.out.println(tcc.CheckContentCharsetEncode(utf8));
System.out.println(utf8);  

String unicode = new String(utf8.getBytes(),tcc.CheckContentCharsetEncode(utf8));   
System.out.println(tcc.CheckContentCharsetEncode(unicode));
System.out.println(unicode);  

String gbk = new String(unicode.getBytes("GBK"));
System.out.println(tcc.CheckContentCharsetEncode(gbk));
System.out.println(gbk);

String iso = new String(unicode.getBytes("iso-8859-1"));
System.out.println(tcc.CheckContentCharsetEncode(iso));
System.out.println(iso);

}catch(Exception e)
{
e.printStackTrace();
}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值