{"id":729,"date":"2025-12-12T10:10:32","date_gmt":"2025-12-12T02:10:32","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=729"},"modified":"2025-12-12T10:10:32","modified_gmt":"2025-12-12T02:10:32","slug":"java-fileinputstream%e8%be%93%e5%85%a5%e6%b5%81_file%e8%bd%acinputstream","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/12\/java-fileinputstream%e8%be%93%e5%85%a5%e6%b5%81_file%e8%bd%acinputstream\/","title":{"rendered":"java FileInputStream(\u8f93\u5165\u6d41)_file\u8f6cinputstream"},"content":{"rendered":"\n<p>\u4e0b\u9762\u63d0\u4f9b\u4e00\u4efd<strong>\u6e05\u6670\u3001\u53ef\u76f4\u63a5\u4f7f\u7528\u7684 Java \u793a\u4f8b<\/strong>\uff0c\u7528\u4e8e\u201c\u5c06 File\uff08\u6216\u6587\u4ef6\u8def\u5f84\uff09\u8f6c\u6362\u4e3a InputStream\uff08FileInputStream\uff09\u201d\uff0c\u5e76\u8bf4\u660e\u5e38\u89c1\u7528\u6cd5\u4e0e\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">1.&nbsp;<strong>File \u8f6c InputStream\uff08\u6700\u76f4\u63a5\u65b9\u5f0f\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nFile file = new File(&quot;path\/to\/your\/file.txt&quot;);\nInputStream inputStream = new FileInputStream(file);\n\n<\/pre><\/div>\n\n\n<p>\u8fd9\u662f\u6700\u6807\u51c6\u3001\u6700\u5e38\u89c1\u7684\u5199\u6cd5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">2.&nbsp;<strong>\u6587\u4ef6\u8def\u5f84 String \u8f6c InputStream<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nInputStream inputStream = new FileInputStream(&quot;path\/to\/your\/file.txt&quot;);\n\n<\/pre><\/div>\n\n\n<p>\u8def\u5f84\u53ef\u4ee5\u662f\u7edd\u5bf9\u8def\u5f84\u6216\u76f8\u5bf9\u8def\u5f84\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">3.&nbsp;<strong>\u4f7f\u7528 try-with-resources \u81ea\u52a8\u5173\u95ed\u6d41<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntry (InputStream inputStream = new FileInputStream(&quot;path\/to\/your\/file.txt&quot;)) {\n    \/\/ \u5728\u8fd9\u91cc\u8bfb\u53d6 inputStream\n} catch (IOException e) {\n    e.printStackTrace();\n}\n\n<\/pre><\/div>\n\n\n<p>\u63a8\u8350\u4f7f\u7528\u8fd9\u79cd\u5199\u6cd5\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u9732\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">4.&nbsp;<strong>\u8bfb\u53d6 InputStream \u793a\u4f8b<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntry (InputStream inputStream = new FileInputStream(&quot;path\/to\/your\/file.txt&quot;)) {\n    byte&#x5B;] buffer = new byte&#x5B;1024];\n    int length;\n    while ((length = inputStream.read(buffer)) != -1) {\n        System.out.println(new String(buffer, 0, length));\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">5.&nbsp;<strong>\u5982\u679c\u4f60\u53ea\u662f\u60f3\u628a\u4e00\u4e2a File \u5bf9\u8c61\u53d8\u6210 InputStream<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic InputStream fileToInputStream(File file) throws FileNotFoundException {\n    return new FileInputStream(file);\n}\n\n<\/pre><\/div>\n\n\n<p>\u8c03\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nInputStream is = fileToInputStream(new File(&quot;a.txt&quot;));\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">6.&nbsp;<strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>FileInputStream \u662f\u963b\u585e\u5f0f IO\uff08BIO\uff09\uff0c\u5982\u679c\u6d89\u53ca\u5927\u91cf\u6587\u4ef6\u64cd\u4f5c\uff0c\u53ef\u8003\u8651 NIO\uff08Files.newInputStream\uff09\u3002<\/li>\n\n\n\n<li>\u521b\u5efa InputStream \u540e\u4e00\u5b9a\u8981\u5173\u95ed\uff0c\u5426\u5219\u53ef\u80fd\u9020\u6210\u6587\u4ef6\u53e5\u67c4\u6cc4\u9732\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5982\u679c\u4f60\u80fd\u544a\u8bc9\u6211<strong>\u4f60\u7684\u5177\u4f53\u4f7f\u7528\u573a\u666f<\/strong>\uff08\u5982\uff1a\u4e0a\u4f20\u6587\u4ef6\u3001\u7f51\u7edc\u4f20\u8f93\u3001\u8f6c\u6362\u6210 MultipartFile\u3001\u8bfb\u53d6\u914d\u7f6e\u7b49\uff09\uff0c\u6211\u53ef\u4ee5\u751f\u6210\u66f4\u9002\u914d\u4f60\u4ee3\u7801\u7ed3\u6784\u7684\u7248\u672c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u63d0\u4f9b\u4e00\u4efd\u6e05\u6670\u3001\u53ef\u76f4\u63a5\u4f7f\u7528\u7684 Java \u793a\u4f8b\uff0c\u7528\u4e8e\u201c\u5c06 File\uff08\u6216\u6587\u4ef6\u8def\u5f84\uff09&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/12\/java-fileinputstream%e8%be%93%e5%85%a5%e6%b5%81_file%e8%bd%acinputstream\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-729","post","type-post","status-publish","format-standard","hentry","category-java"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=729"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/729\/revisions"}],"predecessor-version":[{"id":730,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/729\/revisions\/730"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}