java 编码

来源:互联网 发布:蔡晓红淘宝 编辑:程序博客网 时间:2024/06/06 03:42
public class Code {      public static String code2code(String strIn, String sourceCode,                                  String targetCode) {     String strOut = null;     if (strIn == null || (strIn.trim()).equals(""))       return strIn;     try {       byte[] b = strIn.getBytes(sourceCode);       for(int i = 0 ; i < b.length; i ++){        System.out.print(b[i]+"  ");       }       System.out.println("");       System.out.println("--------------------------------------------------------");       String str=new String(b,sourceCode);       byte[] c=str.getBytes(targetCode);              strOut = new String(c, targetCode);     }     catch (Exception e) {       e.printStackTrace();       return null;     }     return strOut;   }  public static void main(String[] args) {    //System.out.println(new Test().convertStringSms("公司业务"));  String s = "京";        System.out.println(s);  String text = code2code(s,"GBK","UTF-8");  System.out.println(text);  System.out.println(code2code(text,"UTF-8","GBK"));  //System.out.println(System.getProperty("file.encoding"));   }}