{"id":117,"date":"2026-01-09T00:32:12","date_gmt":"2026-01-08T16:32:12","guid":{"rendered":"https:\/\/www.itcainiao.space\/?p=117"},"modified":"2026-01-09T00:33:23","modified_gmt":"2026-01-08T16:33:23","slug":"detailed-explanation-of-io-streams-how-to-easily-understand-input-streams-and-output-streams-z1rdtqc","status":"publish","type":"post","link":"https:\/\/www.itcainiao.space\/index.php\/2026\/01\/09\/detailed-explanation-of-io-streams-how-to-easily-understand-input-streams-and-output-streams-z1rdtqc\/","title":{"rendered":"IO\u6d41\u8be6\u89e3\uff1a\u5982\u4f55\u8f7b\u677e\u7406\u89e3\u8f93\u5165\u6d41\u548c\u8f93\u51fa\u6d41"},"content":{"rendered":"<h1>IO\u6d41\u8be6\u89e3\uff1a\u5982\u4f55\u8f7b\u677e\u7406\u89e3\u8f93\u5165\u6d41\u548c\u8f93\u51fa\u6d41<\/h1>\n<p>\u4f60\u89c9\u5f97 <code>OutputStream<\/code>\u200b\uff08\u5305\u62ec <code>PipedOutputStream<\/code>\u200b\uff09\u548c <code>InputStream<\/code>\u200b \u7684\u65b9\u5411\u96be\u7406\u89e3\uff0c\u6838\u5fc3\u662f\u8981\u6293\u4f4f **\u300c\u4ee5\u5f53\u524d\u7a0b\u5e8f \/ \u7ebf\u7a0b\u4e3a\u89c6\u89d2\u300d** \u8fd9\u4e2a\u5173\u952e\u70b9 \u2014\u2014 \u6240\u6709 IO \u6d41\u7684\u300c\u8f93\u5165 \/ \u8f93\u51fa\u300d\u65b9\u5411\uff0c\u90fd\u662f\u7ad9\u5728<strong>\u4f60\u7684 Java \u7a0b\u5e8f\uff08\u7ebf\u7a0b\uff09<\/strong>  \u672c\u8eab\u6765\u5b9a\u4e49\u7684\uff0c\u800c\u975e\u6570\u636e\u7684\u7edd\u5bf9\u6d41\u5411\u3002<\/p>\n<h3>\u4e00\u3001\u6838\u5fc3\u539f\u5219\uff1a\u4ee5\u300c\u7a0b\u5e8f\u300d\u4e3a\u4e2d\u5fc3\u5224\u65ad\u65b9\u5411<\/h3>\n<p>\u5148\u8bb0\u4f4f\u8fd9\u4e2a\u6838\u5fc3\u903b\u8f91\uff1a<\/p>\n<ul>\n<li>\u200b<strong>Input\uff08\u8f93\u5165\uff09<\/strong> \uff1a\u6570\u636e\u300c\u8fdb\u5165\u300d\u4f60\u7684\u7a0b\u5e8f \u2192 \u7a0b\u5e8f\u4ece\u5916\u90e8\u300c\u8bfb\u53d6\u300d\u6570\u636e\uff08\u7a0b\u5e8f\u662f \u201c\u63a5\u6536\u65b9\u201d\uff09\uff1b<\/li>\n<li>\u200b<strong>Output\uff08\u8f93\u51fa\uff09<\/strong> \uff1a\u6570\u636e\u300c\u79bb\u5f00\u300d\u4f60\u7684\u7a0b\u5e8f \u2192 \u7a0b\u5e8f\u5411\u5916\u90e8\u300c\u5199\u5165\u300d\u6570\u636e\uff08\u7a0b\u5e8f\u662f \u201c\u53d1\u9001\u65b9\u201d\uff09\u3002<\/li>\n<\/ul>\n<p>\u7528\u4e00\u5f20\u8868\u80fd\u5feb\u901f\u533a\u5206\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u6d41\u7c7b\u578b<\/th>\n<th>\u6838\u5fc3\u52a8\u4f5c<\/th>\n<th>\u7a0b\u5e8f\u89d2\u8272<\/th>\n<th>\u901a\u4fd7\u6bd4\u55bb<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>InputStream<\/td>\n<td>read () \u8bfb\u53d6<\/td>\n<td>\u63a5\u6536\u65b9<\/td>\n<td>\u4f60\u4ece\u6c34\u7ba1\u91cc \u201c\u63a5\u6c34\u201d \u5230\u676f\u5b50\u91cc<\/td>\n<\/tr>\n<tr>\n<td>OutputStream<\/td>\n<td>write () \u5199\u5165<\/td>\n<td>\u53d1\u9001\u65b9<\/td>\n<td>\u4f60\u628a\u676f\u5b50\u91cc\u7684\u6c34 \u201c\u5012\u201d \u8fdb\u6c34\u7ba1\u91cc<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4e8c\u3001\u7ed3\u5408\u7ba1\u9053\u6d41\uff08PipedXXX\uff09\u7406\u89e3\uff1a\u7ebf\u7a0b\u89c6\u89d2 + \u6570\u636e\u6d41\u5411<\/h3>\n<p>\u7ba1\u9053\u6d41\u662f\u4e24\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\uff0c\u6b64\u65f6\u9700\u8981\u5206\u522b\u7ad9\u5728\u300c\u5199\u7ebf\u7a0b\u300d\u548c\u300c\u8bfb\u7ebf\u7a0b\u300d\u7684\u89c6\u89d2\u770b\uff0c\u518d\u7ed3\u5408\u6570\u636e\u7684\u6700\u7ec8\u6d41\u5411\uff1a<\/p>\n<h4>1. \u5148\u770b\u5355\u4e2a\u7ebf\u7a0b\u7684\u89c6\u89d2\uff08\u57fa\u7840\uff09<\/h4>\n<p>\u5047\u8bbe\u4f60\u6709\u4e24\u4e2a\u7ebf\u7a0b\uff1a\u200b<strong>\u5199\u7ebf\u7a0b A<\/strong>\u200b\u3001\u200b<strong>\u8bfb\u7ebf\u7a0b B<\/strong>\uff0c\u7ba1\u9053\u6d41\u8fde\u63a5\u4e24\u8005\uff1a<\/p>\n<ul>\n<li>\n<p>\u200b<strong>\u5199\u7ebf\u7a0b A \u6301\u6709 PipedOutputStream<\/strong>\uff1a<br \/>\n\u5bf9\u7ebf\u7a0b A \u6765\u8bf4\uff0c\u5b83\u8981\u628a\u6570\u636e\u300c\u8f93\u51fa\u300d\u5230\u7ba1\u9053\u91cc\uff08\u6570\u636e\u79bb\u5f00\u7ebf\u7a0b A\uff09\u2192 \u6240\u4ee5\u7528 OutputStream\uff08\u5199\u51fa\u53bb\uff09\uff1b<\/p>\n<ul>\n<li>\n<p>\u200b<strong>\u8bfb\u7ebf\u7a0b B \u6301\u6709 PipedInputStream<\/strong>\uff1a<\/p>\n<p>\u5bf9\u7ebf\u7a0b B \u6765\u8bf4\uff0c\u5b83\u8981\u4ece\u7ba1\u9053\u91cc\u300c\u8f93\u5165\u300d\u6570\u636e\uff08\u6570\u636e\u8fdb\u5165\u7ebf\u7a0b B\uff09\u2192 \u6240\u4ee5\u7528 InputStream\uff08\u8bfb\u8fdb\u6765\uff09\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>\u518d\u770b\u6570\u636e\u7684\u6574\u4f53\u6d41\u5411\uff08\u7ed3\u679c\uff09<\/h4>\n<p>\u7ebf\u7a0b A\uff08PipedOutputStream\uff09\u2192 \u7ba1\u9053 \u2192 \u7ebf\u7a0b B\uff08PipedInputStream\uff09\u6570\u636e\u7684\u7edd\u5bf9\u6d41\u5411\u662f A\u2192B\uff0c\u4f46\u300cInput\/Output\u300d\u7684\u547d\u540d\uff0c\u59cb\u7ec8\u662f<strong>\u7ad9\u5728\u6bcf\u4e2a\u7ebf\u7a0b\u81ea\u5df1\u7684\u89d2\u5ea6<\/strong>\u5b9a\u4e49\u7684\uff0c\u800c\u975e\u6570\u636e\u7684\u7edd\u5bf9\u65b9\u5411\u3002<\/p>\n<h3>\u4e09\u3001\u7528\u751f\u6d3b\u4f8b\u5b50\u5f7b\u5e95\u8bb2\u900f\uff08\u7ba1\u9053\u6d41\u573a\u666f\uff09<\/h3>\n<p>\u628a\u4e24\u4e2a\u7ebf\u7a0b\u6bd4\u4f5c\u4e24\u4e2a\u623f\u95f4\uff08A \u623f\u3001B \u623f\uff09\uff0c\u7ba1\u9053\u662f\u8fde\u63a5\u4e24\u4e2a\u623f\u95f4\u7684\u6c34\u7ba1\uff1a<\/p>\n<ul>\n<li>\u200b<strong>A \u623f\uff08\u5199\u7ebf\u7a0b\uff09<\/strong> \uff1a\u4f60\u5728 A \u623f\uff0c\u8981\u628a\u6c34\uff08\u6570\u636e\uff09\u5012\u8fdb\u6c34\u7ba1\u91cc \u2192 \u5bf9 A \u623f\u7684\u4f60\u6765\u8bf4\uff0c\u6c34\u662f \u201c\u8f93\u51fa\u201d\uff08\u79bb\u5f00 A \u623f\uff09\u2192 \u7528 PipedOutputStream\uff08\u5199\u6c34\u7ba1\uff09\uff1b<\/li>\n<li>\u200b<strong>B \u623f\uff08\u8bfb\u7ebf\u7a0b\uff09<\/strong> \uff1a\u4f60\u5728 B \u623f\uff0c\u8981\u4ece\u6c34\u7ba1\u91cc\u63a5\u6c34\uff08\u6570\u636e\uff09\u5230\u676f\u5b50\u91cc \u2192 \u5bf9 B \u623f\u7684\u4f60\u6765\u8bf4\uff0c\u6c34\u662f \u201c\u8f93\u5165\u201d\uff08\u8fdb\u5165 B \u623f\uff09\u2192 \u7528 PipedInputStream\uff08\u8bfb\u6c34\u7ba1\uff09\uff1b<\/li>\n<li>\u6574\u4f53\u6c34\u6d41\u65b9\u5411\uff1aA \u623f \u2192 \u6c34\u7ba1 \u2192 B \u623f\uff0c\u4f46 \u201c\u8f93\u5165 \/ \u8f93\u51fa\u201d \u662f\u7ad9\u5728\u6bcf\u4e2a\u623f\u95f4\uff08\u7ebf\u7a0b\uff09\u81ea\u5df1\u7684\u89c6\u89d2\u8bf4\u7684\uff0c\u4e0d\u662f\u6c34\u7ba1\u7684\u65b9\u5411\u3002<\/li>\n<\/ul>\n<h3>\u56db\u3001\u5bf9\u6bd4\u666e\u901a\u6587\u4ef6\u6d41\uff0c\u5f3a\u5316\u7406\u89e3<\/h3>\n<p>\u7ba1\u9053\u6d41\u7684\u65b9\u5411\u548c\u666e\u901a\u6587\u4ef6\u6d41\u7684\u903b\u8f91\u5b8c\u5168\u4e00\u81f4\uff0c\u53ef\u901a\u8fc7\u6587\u4ef6\u6d41\u9a8c\u8bc1\uff1a<br \/>\njava<\/p>\n<p>\u8fd0\u884c<\/p>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b1\uff1a\u6587\u4ef6\u8f93\u5165\u6d41\uff08InputStream\uff09\u2014\u2014 \u6570\u636e\u4ece\u6587\u4ef6\u8fdb\u5165\u7a0b\u5e8f\nFileInputStream fis = new FileInputStream(&quot;test.txt&quot;);\nfis.read(); \/\/ \u7a0b\u5e8f\u4ece\u6587\u4ef6\u201c\u8f93\u5165\u201d\u6570\u636e\uff08\u8bfb\u6587\u4ef6\uff09\n\n\/\/ \u793a\u4f8b2\uff1a\u6587\u4ef6\u8f93\u51fa\u6d41\uff08OutputStream\uff09\u2014\u2014 \u6570\u636e\u4ece\u7a0b\u5e8f\u8fdb\u5165\u6587\u4ef6\nFileOutputStream fos = new FileOutputStream(&quot;test.txt&quot;);\nfos.write(&quot;hello&quot;.getBytes()); \/\/ \u7a0b\u5e8f\u5411\u6587\u4ef6\u201c\u8f93\u51fa\u201d\u6570\u636e\uff08\u5199\u6587\u4ef6\uff09\n<\/code><\/pre>\n<ul>\n<li>\u5bf9\u7a0b\u5e8f\u6765\u8bf4\uff1a\u8bfb\u6587\u4ef6 = Input\uff0c\u5199\u6587\u4ef6 = Output\uff1b<\/li>\n<li>\u5bf9\u7ba1\u9053\u6d41\u6765\u8bf4\uff1a\u8bfb\u7ba1\u9053 = Input\uff08\u6570\u636e\u8fdb\u7ebf\u7a0b\uff09\uff0c\u5199\u7ba1\u9053 = Output\uff08\u6570\u636e\u51fa\u7ebf\u7a0b\uff09\u3002<\/li>\n<\/ul>\n<h3>\u4e94\u3001\u7ba1\u9053\u6d41\u65b9\u5411\u7684\u6613\u9519\u70b9\u7ea0\u6b63<\/h3>\n<p>\u274c \u9519\u8bef\u7406\u89e3\uff1a\u201cPipedOutputStream \u662f\u6570\u636e\u7684\u7ec8\u70b9\uff0cPipedInputStream \u662f\u8d77\u70b9\u201d\uff1b<\/p>\n<p>\u2705 \u6b63\u786e\u7406\u89e3\uff1a\u201cPipedOutputStream \u662f\u5199\u7ebf\u7a0b\u7684\u8f93\u51fa\u53e3\uff08\u6570\u636e\u79bb\u5f00\u5199\u7ebf\u7a0b\uff09\uff0cPipedInputStream \u662f\u8bfb\u7ebf\u7a0b\u7684\u8f93\u5165\u53e3\uff08\u6570\u636e\u8fdb\u5165\u8bfb\u7ebf\u7a0b\uff09\u201d\u3002<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u7406\u89e3 IO \u6d41\u7684 Input\/Output \u65b9\u5411\uff0c\u53ea\u9700\u8bb0\u4f4f 2 \u4e2a\u5173\u952e\u70b9\uff1a<\/p>\n<ol>\n<li>\n<p>\u200b<strong>\u89c6\u89d2<\/strong>\uff1a\u59cb\u7ec8\u4ee5\u300c\u4f60\u7684\u7a0b\u5e8f \/ \u7ebf\u7a0b\u300d\u4e3a\u4e2d\u5fc3\uff0c\u800c\u975e\u6570\u636e\u7684\u7edd\u5bf9\u6d41\u5411\uff1b<\/p>\n<\/li>\n<li>\n<p>\u200b<strong>\u52a8\u4f5c<\/strong>\uff1a<\/p>\n<ul>\n<li>Input = \u7a0b\u5e8f \/ \u7ebf\u7a0b\u300c\u8bfb\u300d\u6570\u636e\uff08\u6570\u636e\u8fdb\u6765\uff09\uff1b<\/li>\n<li>Output = \u7a0b\u5e8f \/ \u7ebf\u7a0b\u300c\u5199\u300d\u6570\u636e\uff08\u6570\u636e\u51fa\u53bb\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u5bf9\u5e94\u7ba1\u9053\u6d41\uff1a<\/p>\n<ul>\n<li>PipedOutputStream \u2192 \u7ebf\u7a0b \u201c\u5199\u201d \u6570\u636e\u5230\u7ba1\u9053\uff08\u6570\u636e\u51fa\u53bb\uff09= Output\uff1b<\/li>\n<li>PipedInputStream \u2192 \u7ebf\u7a0b \u201c\u8bfb\u201d \u6570\u636e\u4ece\u7ba1\u9053\uff08\u6570\u636e\u8fdb\u6765\uff09= Input\u3002<\/li>\n<\/ul>\n<p>\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u200b<strong>\u8c01\u5199\u6570\u636e\uff0c\u8c01\u7528 Output\uff1b\u8c01\u8bfb\u6570\u636e\uff0c\u8c01\u7528 Input<\/strong>\u3002<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>IO\u6d41\u8be6\u89e3\uff1a\u5982\u4f55\u8f7b\u677e\u7406\u89e3\u8f93\u5165\u6d41\u548c\u8f93\u51fa\u6d41 \u4f60\u89c9\u5f97 OutputStream\u200b\uff08\u5305\u62ec PipedOutputStr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-117","post","type-post","status-publish","format-standard","hentry","category-javajishu"],"_links":{"self":[{"href":"https:\/\/www.itcainiao.space\/index.php\/wp-json\/wp\/v2\/posts\/117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itcainiao.space\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itcainiao.space\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itcainiao.space\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itcainiao.space\/index.php\/wp-json\/wp\/v2\/comments?post=117"}],"version-history":[{"count":2,"href":"https:\/\/www.itcainiao.space\/index.php\/wp-json\/wp\/v2\/posts\/117\/revisions"}],"predecessor-version":[{"id":119,"href":"https:\/\/www.itcainiao.space\/index.php\/wp-json\/wp\/v2\/posts\/117\/revisions\/119"}],"wp:attachment":[{"href":"https:\/\/www.itcainiao.space\/index.php\/wp-json\/wp\/v2\/media?parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itcainiao.space\/index.php\/wp-json\/wp\/v2\/categories?post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itcainiao.space\/index.php\/wp-json\/wp\/v2\/tags?post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}