{"id":744,"date":"2015-06-02T09:58:04","date_gmt":"2015-06-02T01:58:04","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=744"},"modified":"2015-06-02T10:02:39","modified_gmt":"2015-06-02T02:02:39","slug":"%e7%90%86%e8%a7%a3java%e6%8e%a7%e5%88%b6%e5%8f%b0%e8%be%93%e5%85%a5%e5%a5%87%e6%95%b0%e4%b8%aa%e4%b8%ad%e6%96%87%e4%b9%b1%e7%a0%81","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=744","title":{"rendered":"\u7406\u89e3Java\u63a7\u5236\u53f0\u8f93\u5165\u5947\u6570\u4e2a\u4e2d\u6587\u4e71\u7801"},"content":{"rendered":"<p><strong><span style=\"color: #993300;\">Java\u63a7\u5236\u53f0\u8f93\u5165\u4e71\u7801<\/span> <\/strong><br \/>\n\u8fd9\u662f\u5f88\u5e38\u89c1\u7684\u83b7\u53d6\u952e\u76d8\u8f93\u5165\u7684\u4e00\u6bb5Java\u4ee3\u7801\uff0c\u4f46\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u53d1\u73b0\u53ea\u8981\u6211\u8f93\u5165\u7684\u662f\u4e2d\u6587\uff0c\u800c\u4e14\u5b57\u7b26\u6570\u4e3a\u5947\u6570\uff0c\u5c31\u4f1a\u51fa\u73b0\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u4e3a\u4e71\u7801\u7684\u95ee\u9898\uff0c<br \/>\n<!--more--><\/p>\n<p>[java]      BufferedReader reader = new BufferedReader(<br \/>\n\t\t\t\tnew InputStreamReader(System.in));<br \/>\n\t\tString message;<br \/>\n\t\twhile ((message = reader.readLine()) != null) {<br \/>\n\t\t\tSystem.out.println(message);<br \/>\n                }[\/java]<\/p>\n<p>\u4ea7\u751f\u539f\u56e0\uff1a\u56e0\u4e3a\u8fd9\u4e2a\u9879\u76ee\u6587\u4ef6\u7684\u9ed8\u8ba4\u5b57\u7b26\u7f16\u7801\u4e3aUTF-8\uff0c\u6240\u4ee5\u6211\u7684\u63a7\u5236\u53f0\u7684\u5b57\u7b26\u7f16\u7801\u4e5f\u81ea\u52a8\u53d8\u6210\u4e86UTF-8\uff0c\u800c\u952e\u76d8\u7684\u8f93\u5165\u6d41\u7684\u9ed8\u8ba4\u683c\u5f0f\u662fGBK\u683c\u5f0f\uff0c\u8fd9\u6837\u5c31\u9020\u6210\u4e86\u5728GBK\u8f6cUTF-8\u7684\u8fc7\u7a0b\u4e2d\u4ea7\u751f\u7684\u5947\u6570\u4e71\u7801\u9519\u8bef<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\uff1a<br \/>\n\u7b2c\u4e00\u79cd: \u4fee\u6539\u9879\u76ee\u6587\u4ef6\u7f16\u7801\u4e3aGBK,\u63a7\u5236\u53f0\u7684\u5b57\u7b26\u7f16\u7801\u4e5f\u81ea\u52a8\u53d8\u6210\u4e86GBK.<br \/>\n\u7b2c\u4e8c\u79cd:\u5728\u4ee3\u7801\u533a\u57df\u53f3\u952e -&gt; run as -&gt; run configurations -&gt; common(\u53f3\u4fa7) -&gt; console encoding<br \/>\n\u5982\u679c\u51fa\u73b0\u6b64\u9519\u8bef\uff0c\u6b64\u65f6\u7684\u7f16\u7801\u683c\u5f0f\u5e94\u8be5\u662fUTF-8\uff0c\u9009\u62e9Other\uff0c\u8fd9\u65f6\u53ef\u80fd\u6ca1\u6709GBK\u9009\u9879\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u5219\u6267\u884c\u4e4b\u540e\u64cd\u4f5c\u3002<\/p>\n<p><span style=\"color: #993300;\"><strong>\u7406\u89e3\u5e76\u89e3\u51b3GBK\u8f6cUTF-8\u5947\u6570\u4e2d\u6587\u4e71\u7801<\/strong><\/span><\/p>\n<p>\u95ee\u9898\u51fa\u73b0\uff1aGBK\u8f6cUTF-8\u65f6\uff0c\u5947\u6570\u4e2a\u4e2d\u6587\u4f1a\u4e71\u7801\uff0c\u5076\u6570\u4e2a\u4e2d\u6587\u4e0d\u4f1a\u4e71\u7801\u3002<br \/>\n\u4e09\u4e2a\u4e2d\u6587<\/p>\n<p>[java]public static void encodeError() throws UnsupportedEncodingException {<br \/>\n    String gbk = &quot;\u6211\u6765\u4e86&quot;;<br \/>\n    String utf8 = new String(gbk.getBytes(&quot;UTF-8&quot;));  <\/p>\n<p>    \/\/\u6a21\u62dfUTF-8\u7f16\u7801\u7684\u7f51\u7ad9\u663e\u793a<br \/>\n    System.out.println(new String(utf8.getBytes(),&quot;UTF-8&quot;));<br \/>\n}<br \/>\n\/*<br \/>\n\u6211\u6765??<br \/>\n*\/<br \/>\n \u524d\u9762\u4e09\u4e2a\u4e2d\u6587\uff0c\u540e\u9762\u4e00\u4e2a\u4e2d\u6587\uff0c\u90fd\u662f\u5947\u6570<br \/>\nJava\u4ee3\u7801  \u6536\u85cf\u4ee3\u7801<br \/>\npublic static void encodeError2() throws UnsupportedEncodingException {<br \/>\n    String gbk = &quot;\u4eca\u5e74\u662f2011\u5e74&quot;;<br \/>\n    String utf8 = new String(gbk.getBytes(&quot;UTF-8&quot;));  <\/p>\n<p>    \/\/\u6a21\u62dfUTF-8\u7f16\u7801\u7684\u7f51\u7ad9\u663e\u793a<br \/>\n    System.out.println(new String(utf8.getBytes(),&quot;UTF-8&quot;));<br \/>\n}<br \/>\n\/*<br \/>\n\u4eca\u5e74??011??<br \/>\n*\/  [\/java]<\/p>\n<p>\u539f\u56e0\uff1a\u4e3a\u4ec0\u4e48\u53ea\u6709\u5947\u6570\u4e2a\u4e2d\u6587\u624d\u4e71\u7801\uff0c\u5076\u6570\u4e2a\u5374\u4e0d\u4e71\u7801\uff1f\u4e0b\u9762\u6765\u5206\u6790\u539f\u56e0<\/p>\n<p>[java]public static void analyze() throws UnsupportedEncodingException {<br \/>\n    String gbk = &quot;\u6211\u6765\u4e86&quot;;<br \/>\n    String utf8 = new String(gbk.getBytes(&quot;UTF-8&quot;));<br \/>\n    for (byte b : gbk.getBytes(&quot;UTF-8&quot;)) {<br \/>\n        System.out.print(b + &quot; &quot;);<br \/>\n    }<br \/>\n    System.out.println();<br \/>\n    for (byte b : utf8.getBytes()) {<br \/>\n        System.out.print(b + &quot; &quot;);<br \/>\n    }<br \/>\n}<br \/>\n\/*<br \/>\n-26 -120 -111 -26 -99 -91 -28 -70 -122<br \/>\n-26 -120 -111 -26 -99 -91 -28 -70 63<br \/>\n*\/  [\/java]<\/p>\n<p>\u6ce8\u610f\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u4e0d\u540c\uff0c\u4e0a\u9762\u4e00\u884c\u624d\u662f\u6b63\u786e\u7684UTF-8\u7f16\u7801\u3002\u90a3\u4e48\u4e3a\u4ec0\u4e48\u4e0b\u9762\u4e00\u884c\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u662f63\uff0c\u800c\u4e0d\u662f-122\u5462\uff1f\u8fd9\u5c31\u662f\u5bfc\u81f4\u4e71\u7801\u7684\u539f\u56e0\u6240\u5728\u3002<br \/>\nGBK\u7f16\u7801\u662f\u4e00\u4e2a\u4e2d\u65872\u4e2a\u5b57\u8282\uff0c\u800cUTF-8\u7f16\u7801\u662f\u4e00\u4e2a\u4e2d\u65873\u4e2a\u5b57\u8282\uff0c\u5f53\u6211\u4eec\u8c03\u7528getBytes(&#8220;UTF-8&#8243;)\u65b9\u6cd5\u65f6\uff0c\u4f1a\u901a\u8fc7\u8ba1\u7b97\u6765\u589e\u52a0\u5b57\u8282\uff0c\u4f7f\u5f97\u4eceGBK\u76842\u4e2a\u5b57\u8282\u53d8\u6210UTF-8\u5bf9\u5e94\u76843\u4e2a\u5b57\u8282\u3002\u56e0\u6b64\uff0c\u4e0a\u4f8b3\u4e2a\u4e2d\u6587\u8f93\u51fa\u4e869\u4e2a\u5b57\u8282\u3002<\/p>\n<p>\u8fd9\u91cc\u8bb2\u4e00\u4e0b\u600e\u4e48\u901a\u8fc7\u8ba1\u7b97\u589e\u52a0\u5b57\u8282\uff0c\u4e0d\u6df1\u7a76\u7684\u8bfb\u8005\u53ef\u4ee5\u8df3\u8fc7\u6b64\u6bb5\u3002\u4e3a\u4e86\u9192\u76ee\uff0c\u76f4\u63a5\u7528\u4ee3\u7801\u8bb2\u89e3<\/p>\n<p>[java]public static void gbk2Utf() throws UnsupportedEncodingException {<br \/>\n    String gbk = &quot;\u6211\u6765\u4e86&quot;;<br \/>\n    char[] c = gbk.toCharArray();<br \/>\n    byte[] fullByte = new byte[3*c.length];<br \/>\n    for (int i=0; i&lt;c.length; i++) {<br \/>\n        String binary = Integer.toBinaryString(c[i]);<br \/>\n        StringBuffer sb = new StringBuffer();<br \/>\n        int len = 16 &#8211; binary.length();<br \/>\n        \/\/\u524d\u9762\u8865\u96f6<br \/>\n        for(int j=0; j&lt;len; j++){<br \/>\n                sb.append(&quot;0&quot;);<br \/>\n            }<br \/>\n        sb.append(binary);<br \/>\n        \/\/\u589e\u52a0\u4f4d\uff0c\u8fbe\u5230\u523024\u4f4d3\u4e2a\u5b57\u8282<br \/>\n        sb.insert(0, &quot;1110&quot;);<br \/>\n            sb.insert(8, &quot;10&quot;);<br \/>\n            sb.insert(16, &quot;10&quot;);<br \/>\n            fullByte[i*3] = Integer.valueOf(sb.substring(0, 8), 2).byteValue();\/\/\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u521b\u5efa\u6574\u578b<br \/>\n            fullByte[i*3+1] = Integer.valueOf(sb.substring(8, 16), 2).byteValue();<br \/>\n            fullByte[i*3+2] = Integer.valueOf(sb.substring(16, 24), 2).byteValue();<br \/>\n    }<br \/>\n    \/\/\u6a21\u62dfUTF-8\u7f16\u7801\u7684\u7f51\u7ad9\u663e\u793a<br \/>\n    System.out.println(new String(fullByte,&quot;UTF-8&quot;));<br \/>\n}<br \/>\n [\/java]<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u6765\u627e\u51fa\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u662f63\uff0c\u800c\u4e0d\u662f-122\u7684\u539f\u56e0\u3002<\/p>\n<p>[java]public static void analyze2() throws UnsupportedEncodingException {<br \/>\n    String gbk = &quot;\u6211\u6765\u4e86&quot;;<br \/>\n    byte[] utfBytes = gbk.getBytes(&quot;UTF-8&quot;);\/\/\u5f97\u52309\u4e2a\u5b57\u8282<br \/>\n    String utf8 = new String(utfBytes);\/\/\u95ee\u9898\u5c31\u51fa\u5728\u8fd9<br \/>\n    System.out.print(utf8);<br \/>\n}<br \/>\n\/*<br \/>\n\u93b4\u621e\u6f75\u6d5c?<br \/>\n*\/  [\/java]<\/p>\n<p>\u56e0\u4e3a\u6587\u4ef6\u662fGBK\u7f16\u7801\uff0cnew String(utfBytes)\u9ed8\u8ba4\u5c31\u662fnew String(utfBytes,&#8221;GBK&#8221;)\u3002\u5b83\u4f1a2\u4e2a\u5b57\u82822\u4e2a\u5b57\u8282\u5730\u8f6c\u6362\u6210\u5b57\u7b26\uff0c\u5f53\u5b57\u8282\u662f\u5947\u6570\u65f6\u6700\u540e1\u4e2a\u5b57\u8282\u8f6c\u5b57\u7b26\u5c31\u4f1a\u8ba1\u7b97\u9519\u8bef\uff0c\u7136\u540e\u76f4\u63a5\u8d4b\u4e88\u6700\u540e\u8fd9\u4e2a\u5b57\u7b26\u4e3a?\uff0c\u5bf9\u5e94ASCII\u4ee3\u7801\u5c31\u662f63\u3002<\/p>\n<p>\u89e3\u51b3\u95ee\u9898<br \/>\n\u4fdd\u8bc1\u5b57\u8282\u6b63\u786e\u624d\u662f\u786c\u9053\u7406\u3002\u5f53\u8c03\u7528getBytes(&#8220;UTF-8&#8221;)\u8f6c\u6362\u6210\u5b57\u8282\u6570\u7ec4\u540e\uff0c\u521b\u5efaISO-8859-1\u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff0cISO-8859-1\u7f16\u7801\u662f\u4e00\u4e2a\u5b57\u8282\u5bf9\u5e94\u4e00\u4e2a\u5b57\u7b26\uff0c\u56e0\u6b64\u4e0d\u4f1a\u4f7f\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u9519\u8bef\u3002<\/p>\n<p>[java]public static void correctEncode() throws UnsupportedEncodingException {<br \/>\n    String gbk = &quot;\u6211\u6765\u4e86&quot;;<br \/>\n    String iso = new String(gbk.getBytes(&quot;UTF-8&quot;),&quot;ISO-8859-1&quot;);<br \/>\n    for (byte b : iso.getBytes(&quot;ISO-8859-1&quot;)) {<br \/>\n        System.out.print(b + &quot; &quot;);<br \/>\n    }<br \/>\n    System.out.println();  <\/p>\n<p>    \/\/\u6a21\u62dfUTF-8\u7f16\u7801\u7684\u7f51\u7ad9\u663e\u793a<br \/>\n    System.out.println(new String(iso.getBytes(&quot;ISO-8859-1&quot;),&quot;UTF-8&quot;));<br \/>\n}<br \/>\n\/*<br \/>\n-26 -120 -111 -26 -99 -91 -28 -70 -122<br \/>\n\u6211\u6765\u4e86<br \/>\n*\/  [\/java]<\/p>\n<p><span style=\"color: #993300;\"><strong>JAVA\u7f16\u7801\u7814\u7a76\u4e4b\u5b57\u7b26\u8f6c\u5b57\u8282\u5230\u5b57\u8282\u8f6c\u5b57\u7b26<\/strong><\/span><\/p>\n<p>[java]public class Bianma {<br \/>\n    private static void printBytes(byte[] bytes) {<br \/>\n        for (byte b : bytes) {<br \/>\n            printByte(b);<br \/>\n        }<br \/>\n        System.out.println();<br \/>\n    }<br \/>\n    private static void printByte(byte abyte) {<br \/>\n        String hex = &quot;00&quot;+Integer.toHexString((int)abyte);<br \/>\n        System.out.print(hex.substring(hex.length() &#8211; 2) + &quot;\\t&quot;);<br \/>\n    }<br \/>\n    public static void main(String[] args) throws IOException {<br \/>\n        String s = &quot;\u9676&quot;;<br \/>\n        printBytes(s.getBytes(&quot;GBK&quot;));<br \/>\n        printBytes(s.getBytes(&quot;UTF-8&quot;));<br \/>\n        byte b = (byte) System.in.read();<br \/>\n        while (true) {<br \/>\n            printByte(b);<br \/>\n            b = (byte) System.in.read();<br \/>\n        }<br \/>\n    }<br \/>\n}[\/java]<\/p>\n<p>[java]run\uff1a<br \/>\ncc        d5      \/\/ \u9676\u7684GBK\u7f16\u7801<br \/>\ne9        99        b6        \/\/ \u9676\u7684UTF8\u7f16\u7801<br \/>\n\u9676<br \/>\ncc        d5        0a    \/\/ 0a\u662f\u56de\u8f66\u751f\u6210\u7684 [\/java]<\/p>\n<p>[java]byte[] b = new byte[]{new Byte((byte) 0xcc), new Byte((byte) 0xd5)};<br \/>\nSystem.out.println(new String(b, &quot;GBK&quot;));<\/p>\n<p>\/\/\u8f93\u51fa\u4e3a :\u9676[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u63a7\u5236\u53f0\u8f93\u5165\u4e71\u7801 \u8fd9\u662f\u5f88\u5e38\u89c1\u7684\u83b7\u53d6\u952e\u76d8\u8f93\u5165\u7684\u4e00\u6bb5Java\u4ee3\u7801\uff0c\u4f46\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u53d1\u73b0\u53ea\u8981\u6211\u8f93\u5165\u7684\u662f\u4e2d\u6587\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[63],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/744"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=744"}],"version-history":[{"count":4,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/744\/revisions"}],"predecessor-version":[{"id":748,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/744\/revisions\/748"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}