由于字符编码经常导致一些异常问题,而且不是很好定位,一直是写健壮性代码的困惑之一。为此我在调研开源的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();
}
}
}