{"id":918,"date":"2025-12-24T10:51:17","date_gmt":"2025-12-24T02:51:17","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=918"},"modified":"2025-12-24T10:51:17","modified_gmt":"2025-12-24T02:51:17","slug":"java-fileinputstream%e8%be%93%e5%85%a5%e6%b5%81_file%e8%bd%acinputstream-2","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/24\/java-fileinputstream%e8%be%93%e5%85%a5%e6%b5%81_file%e8%bd%acinputstream-2\/","title":{"rendered":"java FileInputStream(\u8f93\u5165\u6d41)_file\u8f6cinputstream"},"content":{"rendered":"\n<p>\u4f60\u60f3\u8981 <strong>\u5c06 <code>File<\/code> \u6216 <code>FileInputStream<\/code> \u8f6c\u6210 <code>InputStream<\/code><\/strong> \u5728 Java \u4e2d\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u56e0\u4e3a <code>FileInputStream<\/code> \u672c\u8eab\u5c31\u5b9e\u73b0\u4e86 <code>InputStream<\/code> \u63a5\u53e3\u3002\u4e0b\u9762\u6211\u6574\u7406\u51e0\u79cd\u5e38\u89c1\u7684\u5199\u6cd5\u548c\u573a\u666f\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1\ufe0f\u20e3 \u76f4\u63a5\u4f7f\u7528 <code>FileInputStream<\/code><\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport java.io.File;\nimport java.io.FileInputStream;\nimport java.io.InputStream;\nimport java.io.IOException;\n\npublic class FileToInputStreamExample {\n    public static void main(String&#x5B;] args) throws IOException {\n        File file = new File(&quot;example.txt&quot;);\n\n        \/\/ FileInputStream \u672c\u8eab\u5c31\u662f InputStream\n        InputStream inputStream = new FileInputStream(file);\n\n        \/\/ \u4f7f\u7528 inputStream\n        int data;\n        while ((data = inputStream.read()) != -1) {\n            System.out.print((char) data);\n        }\n\n        inputStream.close();\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u2705 <strong>\u8bf4\u660e<\/strong>\uff1a<code>FileInputStream<\/code> \u76f4\u63a5\u8d4b\u503c\u7ed9 <code>InputStream<\/code> \u5c31\u53ef\u4ee5\uff0c\u4e0d\u9700\u8981\u989d\u5916\u8f6c\u6362\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2\ufe0f\u20e3 \u5982\u679c\u5df2\u6709 <code>FileInputStream<\/code><\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nFileInputStream fis = new FileInputStream(&quot;example.txt&quot;);\nInputStream is = fis;  \/\/ \u76f4\u63a5\u8d4b\u503c\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>FileInputStream<\/code> \u672c\u8eab\u5c31\u662f <code>InputStream<\/code>\uff0c\u6240\u4ee5\u65e0\u9700\u989d\u5916\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3\ufe0f\u20e3 \u5c06 <code>File<\/code> \u8f6c\u4e3a <code>ByteArrayInputStream<\/code>\uff08\u5728\u5185\u5b58\u4e2d\u5904\u7406\uff09<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u9700\u8981\u628a\u6587\u4ef6\u5185\u5bb9\u8f6c\u4e3a <strong>\u5185\u5b58\u4e2d\u7684 InputStream<\/strong>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport java.io.*;\n\nFile file = new File(&quot;example.txt&quot;);\nbyte&#x5B;] fileContent = new byte&#x5B;(int) file.length()];\nFileInputStream fis = new FileInputStream(file);\nfis.read(fileContent);\nfis.close();\n\n\/\/ \u8f6c\u6210 InputStream\nInputStream inputStream = new ByteArrayInputStream(fileContent);\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u9002\u7528\u4e8e\u9700\u8981\u91cd\u590d\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u7684\u573a\u666f\u3002<\/li>\n\n\n\n<li>\u6ce8\u610f\uff1a\u5927\u6587\u4ef6\u4e0d\u8981\u76f4\u63a5\u8bfb\u5165\u5185\u5b58\uff0c\u4f1a\u6d88\u8017\u5927\u91cf RAM\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4\ufe0f\u20e3 \u4f7f\u7528 NIO \u7684\u65b9\u5f0f\uff08Java 7+\uff09<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport java.io.*;\nimport java.nio.file.Files;\nimport java.nio.file.Paths;\n\nbyte&#x5B;] bytes = Files.readAllBytes(Paths.get(&quot;example.txt&quot;));\nInputStream inputStream = new ByteArrayInputStream(bytes);\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u529f\u80fd\u540c\u7b2c 3 \u79cd\uff0c\u4ee3\u7801\u66f4\u7b80\u6d01\u3002<\/li>\n\n\n\n<li>\u6ce8\u610f\u5927\u6587\u4ef6\u5185\u5b58\u6d88\u8017\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6700\u76f4\u63a5\u65b9\u5f0f<\/strong>\uff1a<code>FileInputStream fis = new FileInputStream(file); InputStream is = fis;<\/code><\/li>\n\n\n\n<li><strong>\u9700\u8981\u5728\u5185\u5b58\u4e2d\u591a\u6b21\u8bfb\u53d6<\/strong>\uff1a\u53ef\u4ee5\u7528 <code>ByteArrayInputStream<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u907f\u514d\u5185\u5b58\u95ee\u9898<\/strong>\uff1a\u76f4\u63a5\u4f7f\u7528 <code>FileInputStream<\/code> \u6d41\u5f0f\u5904\u7406\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4f60\u60f3\u8981 \u5c06 File \u6216 FileInputStream \u8f6c\u6210 InputStr&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/24\/java-fileinputstream%e8%be%93%e5%85%a5%e6%b5%81_file%e8%bd%acinputstream-2\/\">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-918","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\/918","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=918"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/918\/revisions"}],"predecessor-version":[{"id":919,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/918\/revisions\/919"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}