將以前的文章,以及中文問題整理成為一個(gè)類,方便大家可以調(diào)用
/*函數(shù):public String gb(String str) *功能:將字符串以gb2312輸出,解決中文字體亂碼 */ import java.io.UnsupportedEncodingException; public class gb2312 { public gb2312() { } //---------輸出中文------------------------------------------- public String gb2312(String str) { String s1 = null; if(str == null) s1 = null; else try { /** *將字符串str進(jìn)行轉(zhuǎn)換,并且將其最終值賦予s1 */ byte[] tmpbyte=str.getBytes("ISO8859_1"); s1=new String(tmpbyte); } catch(UnsupportedEncodingException unsupportedencodingexception) { } return s1; } //-------------中文內(nèi)碼----------------------------------------------- public String toChinese(String strvalue) { try{ if(strvalue==null) return null; else { strvalue = new String(strvalue.getBytes("gb2312"), "GBK"); return strvalue; } }catch(Exception e){ return null; } } //-----------輸出中文 public static String databasetoChinese(String strvalue) { try{ if(strvalue==null) return null; else { strvalue = new String(strvalue.getBytes("ISO-8859 -1"),"gb2312"); return strvalue; } }catch(Exception e){ return null; } } } 閱讀者如果調(diào)用其中一個(gè)函數(shù)不能完成轉(zhuǎn)碼,可以嘗試gb2312,toChinese 等的轉(zhuǎn)換-) |
|