{"id":35260,"date":"2024-11-26T08:31:27","date_gmt":"2024-11-26T00:31:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35260\/"},"modified":"2024-11-26T08:31:27","modified_gmt":"2024-11-26T00:31:27","slug":"java%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%ad%98%e5%85%a5io%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35260\/","title":{"rendered":"java\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u5b58\u5165io\u4e2d"},"content":{"rendered":"<blockquote><p>\n  java \u4e8c\u7ef4\u6570\u7ec4\u5b58\u50a8\u5230 io \u4e2d\u9700\u8981\u4ee5\u4e0b\u6b65\u9aa4\uff1a1. \u4f7f\u7528 objectoutputstream \u5e8f\u5217\u5316\u6570\u7ec4\uff1b2. \u4f7f\u7528 objectinputstream \u53cd\u5e8f\u5217\u5316\u6570\u7ec4\uff1b3. \u4f7f\u7528 printstream \u5c06\u6570\u7ec4\u5199\u5165\u6587\u672c\u6587\u4ef6\uff08\u7a7a\u683c\u5206\u9694\uff09\uff1b4. \u4f7f\u7528 bufferedwriter \u5c06\u6570\u7ec4\u5199\u5165\u6587\u672c\u6587\u4ef6\uff08\u9017\u53f7\u5206\u9694\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110322482015287.jpg\" class=\"aligncenter\" title=\"java\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u5b58\u5165io\u4e2d\u63d2\u56fe\" alt=\"java\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u5b58\u5165io\u4e2d\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u4e8c\u7ef4\u6570\u7ec4\u5b58\u50a8\u5230 IO \u4e2d<\/strong><\/p>\n<p>\u5c06 Java \u4e8c\u7ef4\u6570\u7ec4\u5b58\u50a8\u5230\u8f93\u5165\/\u8f93\u51fa (IO) \u4e2d\u7684\u8fc7\u7a0b\u6d89\u53ca\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u5e8f\u5217\u5316\u6570\u7ec4<\/strong><\/p>\n<p>\u4f7f\u7528 ObjectOutputStream \u7c7b\u5c06\u4e8c\u7ef4\u6570\u7ec4\u5e8f\u5217\u5316\u4e3a\u5b57\u8282\u6d41\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(\"array.dat\"));\noos.writeObject(array);\noos.close();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u53cd\u5e8f\u5217\u5316\u6570\u7ec4<\/strong><\/p>\n<p>\u4f7f\u7528 ObjectInputStream \u7c7b\u5c06\u5e8f\u5217\u5316\u7684\u5b57\u8282\u6d41\u53cd\u5e8f\u5217\u5316\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<pre>ObjectInputStream ois = new ObjectInputStream(new FileInputStream(\"array.dat\"));\nint[][] array = (int[][]) ois.readObject();\nois.close();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 PrintStream \u5199\u5165\u6570\u7ec4\u5230\u6587\u4ef6\u4e2d<\/strong><\/p>\n<p>\u4f7f\u7528 PrintStream \u7c7b\u5c06\u4e8c\u7ef4\u6570\u7ec4\u5199\u5165\u6587\u672c\u6587\u4ef6\u4e2d\uff0c\u6bcf\u4e2a\u5143\u7d20\u4ee5\u7a7a\u683c\u5206\u9694\u3002<\/p>\n<pre>try (PrintStream ps = new PrintStream(\"array.txt\")) {\n    for (int[] row : array) {\n        for (int element : row) {\n            ps.print(element + \" \");\n        }\n        ps.println();\n    }\n} catch (IOException e) {\n    e.printStackTrace();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 BufferedWriter \u5199\u5165\u6570\u7ec4\u5230\u6587\u4ef6\u4e2d<\/strong><\/p>\n<p>\u4f7f\u7528 BufferedWriter \u7c7b\u5c06\u4e8c\u7ef4\u6570\u7ec4\u5199\u5165\u6587\u672c\u6587\u4ef6\u4e2d\uff0c\u6bcf\u4e2a\u5143\u7d20\u4ee5\u9017\u53f7\u5206\u9694\u3002<\/p>\n<pre>try (BufferedWriter bw = new BufferedWriter(new FileWriter(\"array.csv\"))) {\n    for (int[] row : array) {\n        for (int element : row) {\n            bw.write(element + \",\");\n        }\n        bw.newLine();\n    }\n} catch (IOException e) {\n    e.printStackTrace();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u5b58\u5165io\u4e2d\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u4e8c\u7ef4\u6570\u7ec4\u5b58\u50a8\u5230 io \u4e2d\u9700\u8981\u4ee5\u4e0b\u6b65\u9aa4\uff1a1. \u4f7f\u7528 objectoutputstream \u5e8f\u5217\u5316\u6570\u7ec4\uff1b2. \u4f7f\u7528 objectinputstream \u53cd\u5e8f\u5217\u5316\u6570\u7ec4\uff1b3. \u4f7f\u7528 printstream \u5c06\u6570\u7ec4\u5199\u5165\u6587\u672c\u6587\u4ef6\uff08\u7a7a\u683c\u5206\u9694\uff09\uff1b4. \u4f7f\u7528 bufferedwriter \u5c06\u6570\u7ec4\u5199\u5165\u6587\u672c\u6587\u4ef6\uff08\u9017\u53f7\u5206\u9694\uff09\u3002 \u5982\u4f55\u5c06 Java \u4e8c\u7ef4\u6570\u7ec4\u5b58\u50a8\u5230 IO \u4e2d \u5c06 Java \u4e8c\u7ef4\u6570\u7ec4\u5b58\u50a8\u5230\u8f93\u5165\/\u8f93\u51fa (IO) \u4e2d\u7684\u8fc7\u7a0b\u6d89\u53ca\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u5e8f\u5217\u5316\u6570\u7ec4 \u4f7f\u7528 ObjectOutputStream \u7c7b\u5c06\u4e8c\u7ef4\u6570\u7ec4\u5e8f\u5217\u5316\u4e3a\u5b57\u8282\u6d41\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(&#8220;array.dat&#8221;)); oos.writeObject(array); oos.close(); \u767b\u5f55\u540e\u590d\u5236 2. \u53cd\u5e8f\u5217\u5316\u6570\u7ec4 \u4f7f\u7528 ObjectInputStream \u7c7b\u5c06\u5e8f\u5217\u5316\u7684\u5b57\u8282\u6d41\u53cd\u5e8f\u5217\u5316\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(&#8220;array.dat&#8221;)); int[][] array = [&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-35260","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35260","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=35260"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35260\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}