String.getBytes()

作者在 2008-05-27 20:20:19 发布以下内容
String(byte[] bytes)
             Construct a new String by decoding the specified array of bytes using the platform's default charset.
String(byte[] bytes, String charsetName)
             Construct a new String by decoding the specified array of bytes using the specified charset.

String.getBytes()
          Encodes this Stringinto a sequence of bytes using the platform's default charset, storing the result into a new byte array.
String getBytes (String charsetName)
            Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
 
----------------------------------------------------------------------------------

class HelloUnicode {
    /**
      * main entrance
      * @param args command line arguments
      */

    public static void main(String[] args) {
        String hello = "中文dddd";
        try {
            /*
             * String.getBytes(String charsetName);
             * 使用charsetName的编码对string进行encoding得到byte[],如无参数,使用系统默认编码;
             * new String(byte[], String charsetName);
             * 使用charsetName的编码对byte[]解码,如无参数,使用系统默认参数;
             */


            
             hello = new String(hello.getBytes("utf8"), "utf8");
            System.out.println(hello);
             hello = new String(hello.getBytes());
            System.out.println(hello);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block

             e.printStackTrace();
        }
}

 

技术文章 | 阅读 4386 次
文章评论,共0条
游客请输入验证码