{"id":66708,"date":"2025-05-03T10:13:49","date_gmt":"2025-05-03T02:13:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66708\/"},"modified":"2025-05-03T10:13:49","modified_gmt":"2025-05-03T02:13:49","slug":"%e5%af%b9%e8%b1%a1%e6%80%8e%e4%b9%88%e8%bd%ac%e6%88%90%e5%ad%97%e8%8a%82%e6%95%b0%e7%bb%84-java-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66708\/","title":{"rendered":"\u5bf9\u8c61\u600e\u4e48\u8f6c\u6210\u5b57\u8282\u6570\u7ec4 java"},"content":{"rendered":"<blockquote><p>\n  java \u901a\u8fc7\u5e8f\u5217\u5316\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\uff0c\u5141\u8bb8\u5bf9\u8c61\u72b6\u6001\u5199\u5165\u8f93\u51fa\u6d41\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5bf9\u8c61\u5b9e\u73b0 serializable \u63a5\u53e3\uff0c\u63d0\u4f9b writeobject \u65b9\u6cd5\u3002\u4f7f\u7528 objectoutputstream \u5c06\u5bf9\u8c61\u5199\u5165\u5b57\u8282\u6570\u7ec4\u3002\u4f7f\u7528 objectinputstream \u4ece\u5b57\u8282\u6570\u7ec4\u6062\u590d\u5bf9\u8c61\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111618461274208.jpg\" class=\"aligncenter\" title=\"\u5bf9\u8c61\u600e\u4e48\u8f6c\u6210\u5b57\u8282\u6570\u7ec4 java\u63d2\u56fe\" alt=\"\u5bf9\u8c61\u600e\u4e48\u8f6c\u6210\u5b57\u8282\u6570\u7ec4 java\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5bf9\u8c61\u8f6c\u5b57\u8282\u6570\u7ec4 Java<\/strong><\/p>\n<p>\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u7684\u8fc7\u7a0b\u79f0\u4e3a\u5e8f\u5217\u5316\uff0cJava \u63d0\u4f9b\u4e86 java.io.Serializable \u63a5\u53e3\u6765\u652f\u6301\u5bf9\u8c61\u5e8f\u5217\u5316\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5c06\u5bf9\u8c61\u8f6c\u4e3a\u5b57\u8282\u6570\u7ec4\uff1a<\/p>\n<p><strong>1. \u5b9e\u73b0 Serializable \u63a5\u53e3<\/strong><\/p>\n<p>\u8981\u5e8f\u5217\u5316\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83\u5fc5\u987b\u5b9e\u73b0 Serializable \u63a5\u53e3\u3002\u8fd9\u4f1a\u5f3a\u5236\u5bf9\u8c61\u63d0\u4f9b\u4e00\u4e2a writeObject \u65b9\u6cd5\uff0c\u7528\u4e8e\u5c06\u5bf9\u8c61\u7684\u72b6\u6001\u5199\u5165\u8f93\u51fa\u6d41\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>2. \u4f7f\u7528 ObjectOutputStream<\/strong><\/p>\n<p>ObjectOutputStream \u7c7b\u7528\u4e8e\u5c06\u5bf9\u8c61\u5199\u5165\u5b57\u8282\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u6b65\u9aa4\uff1a<\/p>\n<pre>import java.io.ByteArrayOutputStream;\nimport java.io.ObjectOutputStream;\n\npublic class ObjectToByteArray {\n\n    public static byte[] objectToByteArray(Serializable object) throws IOException {\n        ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        ObjectOutputStream oos = new ObjectOutputStream(baos);\n        oos.writeObject(object);\n        oos.close();\n        return baos.toByteArray();\n    }\n\n    public static void main(String[] args) {\n        Employee employee = new Employee(\"John Doe\", 12345);\n        byte[] bytes = objectToByteArray(employee);\n        \/\/ \u4f7f\u7528 bytes \u8fdb\u884c\u64cd\u4f5c...\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u53cd\u5e8f\u5217\u5316\u5b57\u8282\u6570\u7ec4<\/strong><\/p>\n<p>\u8981\u6062\u590d\u5df2\u5e8f\u5217\u5316\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528 ObjectInputStream \u7c7b\u3002<\/p>\n<pre>import java.io.ByteArrayInputStream;\nimport java.io.ObjectInputStream;\n\npublic class ObjectFromByteArray {\n\n    public static &lt;T extends Serializable&gt; T byteToObject(byte[] bytes) throws IOException, ClassNotFoundException {\n        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);\n        ObjectInputStream ois = new ObjectInputStream(bais);\n        T object = (T) ois.readObject();\n        ois.close();\n        return object;\n    }\n\n    public static void main(String[] args) {\n        byte[] bytes = ... \/\/ \u4ece\u4e4b\u524d\u5e8f\u5217\u5316\u83b7\u5f97\n        Employee employee = byteToObject(bytes);\n        \/\/ \u4f7f\u7528 employee \u8fdb\u884c\u64cd\u4f5c...\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5bf9\u8c61\u600e\u4e48\u8f6c\u6210\u5b57\u8282\u6570\u7ec4 java\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>java \u901a\u8fc7\u5e8f\u5217\u5316\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\uff0c\u5141\u8bb8\u5bf9\u8c61\u72b6\u6001\u5199\u5165\u8f93\u51fa\u6d41\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5bf9\u8c61\u5b9e\u73b0 serializable \u63a5\u53e3\uff0c\u63d0\u4f9b writeobject \u65b9\u6cd5\u3002\u4f7f\u7528 objectoutputstream \u5c06\u5bf9\u8c61\u5199\u5165\u5b57\u8282\u6570\u7ec4\u3002\u4f7f\u7528 objectinputstream \u4ece\u5b57\u8282\u6570\u7ec4\u6062\u590d\u5bf9\u8c61\u3002 \u5bf9\u8c61\u8f6c\u5b57\u8282\u6570\u7ec4 Java \u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u7684\u8fc7\u7a0b\u79f0\u4e3a\u5e8f\u5217\u5316\uff0cJava \u63d0\u4f9b\u4e86 java.io.Serializable \u63a5\u53e3\u6765\u652f\u6301\u5bf9\u8c61\u5e8f\u5217\u5316\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5c06\u5bf9\u8c61\u8f6c\u4e3a\u5b57\u8282\u6570\u7ec4\uff1a 1. \u5b9e\u73b0 Serializable \u63a5\u53e3 \u8981\u5e8f\u5217\u5316\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83\u5fc5\u987b\u5b9e\u73b0 Serializable \u63a5\u53e3\u3002\u8fd9\u4f1a\u5f3a\u5236\u5bf9\u8c61\u63d0\u4f9b\u4e00\u4e2a writeObject \u65b9\u6cd5\uff0c\u7528\u4e8e\u5c06\u5bf9\u8c61\u7684\u72b6\u6001\u5199\u5165\u8f93\u51fa\u6d41\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 2. \u4f7f\u7528 ObjectOutputStream ObjectOutputStream \u7c7b\u7528\u4e8e\u5c06\u5bf9\u8c61\u5199\u5165\u5b57\u8282\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u6b65\u9aa4\uff1a import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; public class ObjectToByteArray { public static byte[] objectToByteArray(Serializable object) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = [&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-66708","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66708","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=66708"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66708\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}