{"id":744,"date":"2015-06-02T09:58:04","date_gmt":"2015-06-02T01:58:04","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=744"},"modified":"2026-08-18T12:09:34","modified_gmt":"2026-08-18T04:09:34","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":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u8fd9\u662f\u5f88\u5e38\u89c1\u7684\u4e00\u6bb5\u4ece\u952e\u76d8\u8bfb\u5165\u7684 Java \u4ee3\u7801\u3002\u8fd0\u884c\u65f6\u53ea\u8981\u8f93\u5165\u7684\u662f\u4e2d\u6587\uff0c\u800c\u4e14\u5b57\u7b26\u4e2a\u6570\u4e3a<strong style=\"color: #4870ac;\">\u5947\u6570<\/strong>\uff0c\u6700\u540e\u4e00\u4e2a\u5b57\u5c31\u4f1a\u4e71\u7801\u3002<\/p>\n<p><!--more--><\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">BufferedReader reader = new BufferedReader(\n        new InputStreamReader(System.in));\nString message;\nwhile ((message = reader.readLine()) != null) {\n    System.out.println(message);\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u539f\u56e0\uff1a\u9879\u76ee\u9ed8\u8ba4\u5b57\u7b26\u7f16\u7801\u662f UTF-8\uff0c\u63a7\u5236\u53f0\u4e5f\u8ddf\u7740\u53d8\u6210 UTF-8\uff0c\u800c\u952e\u76d8\u8f93\u5165\u6d41\u9ed8\u8ba4\u662f GBK\u3002GBK \u8f6c UTF-8 \u65f6\u5c31\u4f1a\u51fa\u73b0\u300c\u5947\u6570\u4e2a\u4e2d\u6587\u4e71\u7801\u300d\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u63a7\u5236\u53f0\u4fa7\u7684\u4e24\u79cd\u6539\u6cd5<\/h2>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u628a\u9879\u76ee\u6587\u4ef6\u7f16\u7801\u6539\u6210 GBK\uff0c\u63a7\u5236\u53f0\u7f16\u7801\u4f1a\u8ddf\u7740\u53d8\u6210 GBK\u3002<\/section>\n<\/li>\n<li>\n<section>\u4ee3\u7801\u533a\u57df\u53f3\u952e \u2192 Run As \u2192 Run Configurations \u2192 Common \u2192 Console encoding\u3002\u82e5\u662f UTF-8\uff0c\u9009 Other\uff1b\u6ca1\u6709 GBK \u9009\u9879\u65f6\u518d\u624b\u52a8\u6307\u5b9a\u3002<\/section>\n<\/li>\n<\/ul>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u4e3a\u4f55\u53ea\u6709\u5947\u6570\u4e2a\u4e2d\u6587\u4e71\u7801<\/h2>\n<p style=\"margin: 1em 0px;\">GBK \u8f6c UTF-8 \u65f6\uff0c\u5947\u6570\u4e2a\u4e2d\u6587\u4f1a\u4e71\u7801\uff0c\u5076\u6570\u4e2a\u4e0d\u4f1a\u3002\u4e09\u4e2a\u4e2d\u6587\u3001\u540e\u9762\u518d\u8ddf\u5947\u6570\u4e2a\u4e2d\u6587\u90fd\u4f1a\u51fa\u95ee\u9898\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public static void encodeError() throws UnsupportedEncodingException {\n    String gbk = \"\u6211\u6765\u4e86\";\n    String utf8 = new String(gbk.getBytes(\"UTF-8\"));\n    \/\/ \u6a21\u62df UTF-8 \u7f16\u7801\u7684\u7f51\u7ad9\u663e\u793a\n    System.out.println(new String(utf8.getBytes(), \"UTF-8\"));\n}\n\/\/ \u8f93\u51fa\uff1a\u6211\u6765??\n\npublic static void encodeError2() throws UnsupportedEncodingException {\n    String gbk = \"\u4eca\u5e74\u662f2011\u5e74\";\n    String utf8 = new String(gbk.getBytes(\"UTF-8\"));\n    System.out.println(new String(utf8.getBytes(), \"UTF-8\"));\n}\n\/\/ \u8f93\u51fa\uff1a\u4eca\u5e74??011??<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u770b\u5b57\u8282\u5c31\u80fd\u5b9a\u4f4d\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u88ab\u6539\u6389\u4e86\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public static void analyze() throws UnsupportedEncodingException {\n    String gbk = \"\u6211\u6765\u4e86\";\n    String utf8 = new String(gbk.getBytes(\"UTF-8\"));\n    for (byte b : gbk.getBytes(\"UTF-8\")) {\n        System.out.print(b + \" \");\n    }\n    System.out.println();\n    for (byte b : utf8.getBytes()) {\n        System.out.print(b + \" \");\n    }\n}\n\/\/ -26 -120 -111 -26 -99 -91 -28 -70 -122\n\/\/ -26 -120 -111 -26 -99 -91 -28 -70 63<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u4e0a\u9762\u4e00\u884c\u624d\u662f\u6b63\u786e\u7684 UTF-8\u3002\u4e0b\u9762\u4e00\u884c\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u53d8\u6210 63\uff08ASCII \u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">?<\/code>\uff09\uff0c\u4e71\u7801\u5c31\u51fa\u5728\u8fd9\u91cc\u3002<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u7f16\u7801<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u4e00\u4e2a\u4e2d\u6587\u5360\u7528<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u6ce8\u610f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">GBK<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">2 \u5b57\u8282<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6309\u53cc\u5b57\u8282\u5207\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">UTF-8<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">3 \u5b57\u8282<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">3 \u4e2a\u4e2d\u6587\u5171 9 \u5b57\u8282\uff0c\u5947\u6570<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">ISO-8859-1<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">1 \u5b57\u8282 1 \u5b57\u7b26<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4e0d\u4f1a\u541e\u6389\u6700\u540e\u4e00\u4e2a\u5b57\u8282<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"margin: 1em 0px;\">\u8c03\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">getBytes(\"UTF-8\")<\/code> \u4f1a\u628a GBK \u7684 2 \u5b57\u8282\u6269\u6210 UTF-8 \u7684 3 \u5b57\u8282\u3002\u6e90\u6587\u4ef6\u82e5\u662f GBK\uff0c<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">new String(utfBytes)<\/code> \u5b9e\u9645\u662f\u6309 GBK \u6bcf 2 \u5b57\u8282\u5207\u4e00\u5200\uff1b\u5b57\u8282\u6570\u4e3a\u5947\u6570\u65f6\uff0c\u6700\u540e 1 \u5b57\u8282\u8f6c\u4e0d\u6210\u5408\u6cd5\u5b57\u7b26\uff0c\u5c31\u88ab\u5199\u6210 ?\uff0863\uff09\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public static void analyze2() throws UnsupportedEncodingException {\n    String gbk = \"\u6211\u6765\u4e86\";\n    byte[] utfBytes = gbk.getBytes(\"UTF-8\"); \/\/ 9 \u4e2a\u5b57\u8282\n    String utf8 = new String(utfBytes);      \/\/ \u95ee\u9898\u5c31\u51fa\u5728\u8fd9\n    System.out.print(utf8);\n}\n\/\/ \u93b4\u621e\u6f75\u6d5c?<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u4fdd\u8bc1\u5b57\u8282\u4e0d\u88ab\u622a\u65ad<\/h2>\n<p style=\"margin: 1em 0px;\">getBytes(&#8220;UTF-8&#8221;) \u4e4b\u540e\uff0c\u7528 ISO-8859-1 \u5efa\u5b57\u7b26\u4e32\uff1a\u4e00\u4e2a\u5b57\u8282\u5bf9\u5e94\u4e00\u4e2a\u5b57\u7b26\uff0c\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u4e0d\u4f1a\u9519\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public static void correctEncode() throws UnsupportedEncodingException {\n    String gbk = \"\u6211\u6765\u4e86\";\n    String iso = new String(gbk.getBytes(\"UTF-8\"), \"ISO-8859-1\");\n    for (byte b : iso.getBytes(\"ISO-8859-1\")) {\n        System.out.print(b + \" \");\n    }\n    System.out.println();\n    System.out.println(new String(iso.getBytes(\"ISO-8859-1\"), \"UTF-8\"));\n}\n\/\/ -26 -120 -111 -26 -99 -91 -28 -70 -122\n\/\/ \u6211\u6765\u4e86<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u56db\u3001\u5b57\u7b26\u8f6c\u5b57\u8282\u5bf9\u7167<\/h2>\n<p style=\"margin: 1em 0px;\">\u300c\u9676\u300d\u5728 GBK \u662f cc d5\uff0c\u5728 UTF-8 \u662f e9 99 b6\u3002\u7528\u6307\u5b9a\u5b57\u7b26\u96c6\u8fd8\u539f\u5c31\u80fd\u5f97\u5230\u539f\u5b57\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">byte[] b = new byte[]{(byte) 0xcc, (byte) 0xd5};\nSystem.out.println(new String(b, \"GBK\"));\n\/\/ \u8f93\u51fa\uff1a\u9676<\/code><\/pre>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u5947\u6570\u4e2a\u4e2d\u6587\u4e71\u7801\u662f\u56e0\u4e3a UTF-8 \u5b57\u8282\u6570\u662f\u5947\u6570\uff0c\u5374\u88ab\u9ed8\u8ba4 GBK \u6309\u53cc\u5b57\u8282\u53bb\u5207\uff1b\u7528 ISO-8859-1 \u6216\u7edf\u4e00\u7f16\u7801\u624d\u80fd\u4fdd\u4f4f\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u5f88\u5e38\u89c1\u7684\u4e00\u6bb5\u4ece\u952e\u76d8\u8bfb\u5165\u7684 Java \u4ee3\u7801\u3002\u8fd0\u884c\u65f6\u53ea\u8981\u8f93\u5165\u7684\u662f\u4e2d\u6587\uff0c\u800c\u4e14\u5b57\u7b26\u4e2a\u6570\u4e3a\u5947\u6570\uff0c\u6700\u540e\u4e00\u4e2a\u5b57\u5c31\u4f1a\u4e71\u7801\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[41,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":3,"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}]}}