{"id":1311,"date":"2022-10-29T22:53:04","date_gmt":"2022-10-29T14:53:04","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1311"},"modified":"2022-11-27T12:39:46","modified_gmt":"2022-11-27T04:39:46","slug":"io%e6%b5%81-%e4%bd%bf%e7%94%a8fileinputstream%e4%b8%8d%e8%83%bd%e8%af%bb%e5%8f%96%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6%e7%9a%84%e6%b5%8b%e8%af%95%e3%80%81%e4%bd%bf%e7%94%a8fileinputstream%e5%92%8cfileo","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1311","title":{"rendered":"IO\u6d41-\u4f7f\u7528FileInputStream\u4e0d\u80fd\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u6d4b\u8bd5\u3001\u4f7f\u7528FileInputStream\u548cFileOutputStream\u8bfb\u5199\u975e\u6587\u672c\u6587\u4ef6\u3001\u4f7f\u7528FileInputStream\u548cFileOutputStream\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u6d4b\u8bd5"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P592;\n\nimport org.junit.jupiter.api.Test;\n\nimport java.io.*;\n\n\/**\n * \u6d4b\u8bd5FileInputStream\u548cFileOutputStream\u7684\u4f7f\u7528\n *\n * \u7ed3\u8bba\uff1a\n * 1. \u5bf9\u4e8e\u6587\u672c\u6587\u4ef6(.txt,.java,.c,.cpp)\uff0c\u4f7f\u7528\u5b57\u7b26\u6d41\u5904\u7406\n * 2. \u5bf9\u4e8e\u975e\u6587\u672c\u6587\u4ef6(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...)\uff0c\u4f7f\u7528\u5b57\u8282\u6d41\u5904\u7406\n *\n * @author RichieZhang\n * @create 2022-10-29 \u4e0b\u5348 10:01\n *\/\npublic class FileInputOutputStreamTest {\n\n    \/\/\u4f7f\u7528\u5b57\u8282\u6d41FileInputStream\u5904\u7406\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u80fd\u51fa\u73b0\u4e71\u7801\u3002\n    @Test\n    public void testFileInputStream() {\n        FileInputStream fis = null;\n\n        try {\n            \/\/1. \u9020\u6587\u4ef6\n            File file = new File(\"hello.txt\");\n\n            \/\/2. \u9020\u6d41\n            fis = new FileInputStream(file);\n\n            \/\/3. \u8bfb\u6570\u636e\n            byte&#91;] buffer = new byte&#91;5];\n            int len;\/\/\u8bb0\u5f55\u6bcf\u6b21\u8bfb\u53d6\u7684\u5b57\u8282\u7684\u4e2a\u6570\n            while ((len = fis.read(buffer)) != -1){\n\n                String str = new String(buffer,0,len);\n                System.out.print(str);\n            }\n        } catch (IOException e) {\n            e.printStackTrace();\n        } finally {\n            if(fis != null){\n                \/\/4. \u5173\u95ed\u8d44\u6e90\n                try {\n                    fis.close();\n                } catch (IOException e) {\n                    e.printStackTrace();\n                }\n\n            }\n        }\n    }\n\n    \/*\n    \u5b9e\u73b0\u5bf9\u56fe\u7247\u7684\u590d\u5236\u64cd\u4f5c\n     *\/\n    @Test\n    public void testFileInputOutputStream() {\n        FileInputStream fis = null;\n        FileOutputStream fos = null;\n        try {\n            \/\/\n            File strFile = new File(\"\u7231\u60c5\u4e0e\u53cb\u60c5.jpg\");\n            File destFile = new File(\"\u7231\u60c5\u4e0e\u53cb\u60c52.jpg\");\n\n            \/\/\n            fis = new FileInputStream(strFile);\n            fos = new FileOutputStream(destFile);\n\n            \/\/\u590d\u5236\u7684\u8fc7\u7a0b\n            byte&#91;] buffer = new byte&#91;5];\n            int len;\n            while ((len = fis.read(buffer)) != -1){\n                fos.write(buffer,0,len);\n            }\n\n        } catch (IOException e) {\n            e.printStackTrace();\n        } finally {\n            if (fos != null){\n                \/\/\n                try {\n                    fos.close();\n                } catch (IOException e) {\n                    e.printStackTrace();\n                }\n            }\n            if (fis != null){\n                try {\n                    fis.close();\n                } catch (IOException e) {\n                    e.printStackTrace();\n                }\n            }\n        }\n\n    }\n\n    \/\/\u6307\u5b9a\u8def\u5f84\u4e0b\u6587\u4ef6\u7684\u590d\u5236\n    public void copyFile(String srcPath, String destPath){\n        FileInputStream fis = null;\n        FileOutputStream fos = null;\n        try {\n            \/\/\n            File strFile = new File(srcPath);\n            File destFile = new File(destPath);\n\n            \/\/\n            fis = new FileInputStream(strFile);\n            fos = new FileOutputStream(destFile);\n\n            \/\/\u590d\u5236\u7684\u8fc7\u7a0b\n            byte&#91;] buffer = new byte&#91;1024];\n            int len;\n            while ((len = fis.read(buffer)) != -1){\n                fos.write(buffer,0,len);\n            }\n\n        } catch (IOException e) {\n            e.printStackTrace();\n        } finally {\n            if (fos != null){\n                \/\/\n                try {\n                    fos.close();\n                } catch (IOException e) {\n                    e.printStackTrace();\n                }\n            }\n            if (fis != null){\n                try {\n                    fis.close();\n                } catch (IOException e) {\n                    e.printStackTrace();\n                }\n            }\n        }\n    }\n\n    @Test\n    public void testCopyFile(){\n\n        long start = System.currentTimeMillis();\n\n        String srcPath = \"C:\\\\Users\\\\ZRich\\\\Documents\\\\\u6211\u7684\u6587\u6863\\\\PDF.pdf\";\n        String destPath = \"PDF.pdf\";\n\n\/\/        String srcPath = \"hello.txt\";\n\/\/        String destPath = \"hello3.txt\";\n\n        copyFile(srcPath,destPath);\n\n        long end = System.currentTimeMillis();\n\n        System.out.println(\"\u590d\u5236\u64cd\u4f5c\u82b1\u8d39\u7684\u65f6\u95f4\u4e3a\uff1a\" + (end - start));\/\/614\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-1311","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1311"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1311"}],"version-history":[{"count":2,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1311\/revisions"}],"predecessor-version":[{"id":1313,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1311\/revisions\/1313"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}