{"id":64641,"date":"2025-05-03T09:34:25","date_gmt":"2025-05-03T01:34:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64641\/"},"modified":"2025-05-03T09:34:25","modified_gmt":"2025-05-03T01:34:25","slug":"java%e6%80%8e%e4%b9%88%e7%94%a8%e5%ad%97%e8%8a%82%e6%95%b0%e7%bb%84%e5%86%99%e5%90%8d%e5%ad%97-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64641\/","title":{"rendered":"java\u600e\u4e48\u7528\u5b57\u8282\u6570\u7ec4\u5199\u540d\u5b57"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528\u5b57\u8282\u6570\u7ec4\u5199\u5165\u59d3\u540d\uff1a\u521b\u5efa\u5b57\u8282\u6570\u7ec4\u5c06\u59d3\u540d\u7f16\u7801\u4e3a\u5b57\u8282\u5c06\u5b57\u8282\u5199\u5165\u5b57\u8282\u6570\u7ec4\u4fdd\u5b58\u6216\u5904\u7406\u5b57\u8282\u6570\u7ec4\u53ef\u9006\u64cd\u4f5c\uff1a\u4ece\u5b57\u8282\u6570\u7ec4\u8bfb\u53d6\u59d3\u540d\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/04\/2024110404485194464.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u7528\u5b57\u8282\u6570\u7ec4\u5199\u540d\u5b57\u63d2\u56fe\" alt=\"java\u600e\u4e48\u7528\u5b57\u8282\u6570\u7ec4\u5199\u540d\u5b57\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u5b57\u8282\u6570\u7ec4\u5199\u5165\u59d3\u540d<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u8282\u6570\u7ec4\uff08byte[]\uff09\u6765\u6709\u6548\u5730\u5b58\u50a8\u548c\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5305\u62ec\u6587\u672c\u6570\u636e\u3002\u8981\u4f7f\u7528\u5b57\u8282\u6570\u7ec4\u5199\u5165\u59d3\u540d\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u5b57\u8282\u6570\u7ec4<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u9002\u5f53\u5927\u5c0f\u7684\u5b57\u8282\u6570\u7ec4\u6765\u5bb9\u7eb3\u59d3\u540d\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u5b57\u8282\u6570\u7ec4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>byte[] nameBytes = new byte[255];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u5c06\u59d3\u540d\u7f16\u7801\u4e3a\u5b57\u8282<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u5c06\u59d3\u540d\u7f16\u7801\u4e3a\u5b57\u8282\u6570\u7ec4\u3002\u53ef\u4ee5\u4f7f\u7528 Java \u4e2d\u7684 getBytes() \u65b9\u6cd5\u6765\u5b8c\u6210\u6b64\u4efb\u52a1\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u59d3\u540d\u5b57\u8282\u8868\u793a\u7684\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<pre>String name = \"John Doe\";\nnameBytes = name.getBytes();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u5199\u5165\u5b57\u8282\u6570\u7ec4<\/strong><\/p>\n<p>\u73b0\u5728\uff0c\u53ef\u4ee5\u5c06\u59d3\u540d\u5b57\u8282\u5199\u5165\u5b57\u8282\u6570\u7ec4\u3002\u53ef\u4ee5\u5faa\u73af\u904d\u5386 nameBytes \u6570\u7ec4\u5e76\u5c06\u5176\u5143\u7d20\u5206\u914d\u7ed9\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<pre>int index = 0;\nfor (byte b : nameBytes) {\n    data[index++] = b;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4fdd\u5b58\u6216\u5904\u7406\u5b57\u8282\u6570\u7ec4<\/strong><\/p>\n<p>\u6700\u540e\uff0c\u53ef\u4ee5\u5c06\u5b57\u8282\u6570\u7ec4\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u6216\u5c06\u5176\u7528\u4e8e\u5176\u4ed6\u76ee\u7684\u3002\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u8f93\u51fa\u6d41\u6216\u7f51\u7edc\u8fde\u63a5\u6765\u4fdd\u5b58\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<pre>\/\/ \u4f7f\u7528\u6587\u4ef6\u8f93\u51fa\u6d41\u4fdd\u5b58\u5b57\u8282\u6570\u7ec4\nFileOutputStream fileOutputStream = new FileOutputStream(\"name.txt\");\nfileOutputStream.write(data);\nfileOutputStream.close();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u4ece\u5b57\u8282\u6570\u7ec4\u4e2d\u8bfb\u53d6\u59d3\u540d<\/strong><\/p>\n<p>\u8981\u4ece\u5b57\u8282\u6570\u7ec4\u4e2d\u8bfb\u53d6\u59d3\u540d\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u9006\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 new String(byte[]) \u6784\u9020\u51fd\u6570\u4ece\u5b57\u8282\u6570\u7ec4\u4e2d\u521b\u5efa\u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u4f7f\u7528 String.getBytes() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p>\u6309\u7167\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5728 Java \u4e2d\u4f7f\u7528\u5b57\u8282\u6570\u7ec4\u6709\u6548\u5730\u5199\u5165\u548c\u8bfb\u53d6\u59d3\u540d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u7528\u5b57\u8282\u6570\u7ec4\u5199\u540d\u5b57\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u5b57\u8282\u6570\u7ec4\u5199\u5165\u59d3\u540d\uff1a\u521b\u5efa\u5b57\u8282\u6570\u7ec4\u5c06\u59d3\u540d\u7f16\u7801\u4e3a\u5b57\u8282\u5c06\u5b57\u8282\u5199\u5165\u5b57\u8282\u6570\u7ec4\u4fdd\u5b58\u6216\u5904\u7406\u5b57\u8282\u6570\u7ec4\u53ef\u9006\u64cd\u4f5c\uff1a\u4ece\u5b57\u8282\u6570\u7ec4\u8bfb\u53d6\u59d3\u540d \u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u5b57\u8282\u6570\u7ec4\u5199\u5165\u59d3\u540d \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u8282\u6570\u7ec4\uff08byte[]\uff09\u6765\u6709\u6548\u5730\u5b58\u50a8\u548c\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5305\u62ec\u6587\u672c\u6570\u636e\u3002\u8981\u4f7f\u7528\u5b57\u8282\u6570\u7ec4\u5199\u5165\u59d3\u540d\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a 1. \u521b\u5efa\u5b57\u8282\u6570\u7ec4 \u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u9002\u5f53\u5927\u5c0f\u7684\u5b57\u8282\u6570\u7ec4\u6765\u5bb9\u7eb3\u59d3\u540d\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u5b57\u8282\u6570\u7ec4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b byte[] nameBytes = new byte[255]; \u767b\u5f55\u540e\u590d\u5236 2. \u5c06\u59d3\u540d\u7f16\u7801\u4e3a\u5b57\u8282 \u63a5\u4e0b\u6765\uff0c\u9700\u8981\u5c06\u59d3\u540d\u7f16\u7801\u4e3a\u5b57\u8282\u6570\u7ec4\u3002\u53ef\u4ee5\u4f7f\u7528 Java \u4e2d\u7684 getBytes() \u65b9\u6cd5\u6765\u5b8c\u6210\u6b64\u4efb\u52a1\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u59d3\u540d\u5b57\u8282\u8868\u793a\u7684\u5b57\u8282\u6570\u7ec4\u3002 String name = &#8220;John Doe&#8221;; nameBytes = name.getBytes(); \u767b\u5f55\u540e\u590d\u5236 3. \u5199\u5165\u5b57\u8282\u6570\u7ec4 \u73b0\u5728\uff0c\u53ef\u4ee5\u5c06\u59d3\u540d\u5b57\u8282\u5199\u5165\u5b57\u8282\u6570\u7ec4\u3002\u53ef\u4ee5\u5faa\u73af\u904d\u5386 nameBytes \u6570\u7ec4\u5e76\u5c06\u5176\u5143\u7d20\u5206\u914d\u7ed9\u5b57\u8282\u6570\u7ec4\u3002 int index = 0; for (byte b : nameBytes) { data[index++] = b; } \u767b\u5f55\u540e\u590d\u5236 4. \u4fdd\u5b58\u6216\u5904\u7406\u5b57\u8282\u6570\u7ec4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-64641","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=64641"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64641\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}