Java中文识别问题完全解决办法[二]

来源:互联网 发布:veket linux 编辑:程序博客网 时间:2024/06/06 18:21

版权归 石太祥 (ealpha(AT)msn(DOT)com)所有;

联系:   msn: ealpha(AT)msn(DOT)com

            QQ : 9690501

==============================================================================

上篇写了一篇文章,说中文解决方法的,这一篇在做一个小的整理,

==============================================================================

将以前的文章,以及中文问题整理成为一个类,方便大家可以调用


/*函数: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进行转换,并且将其最终值赋予s1
                */

                 byte[] tmpbyte=str.getBytes("ISO8859_1");
     s1=new String(tmpbyte);
            }
            catch(UnsupportedEncodingException unsupportedencodingexception) { }
        return s1;
    }

     //-------------中文内码-----------------------------------------------
     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;
                }
          }
}

 

阅读者如果调用其中一个函数不能完成转码,可以尝试gb2312,toChinese 等的转换-)