﻿{"id":283,"date":"2017-03-20T20:57:45","date_gmt":"2017-03-20T12:57:45","guid":{"rendered":"http:\/\/www.chenweikang.top\/?p=283"},"modified":"2017-09-19T11:37:17","modified_gmt":"2017-09-19T03:37:17","slug":"node-js-%e6%96%87%e4%bb%b6%e6%93%8d%e4%bd%9c%e6%a8%a1%e5%9d%97fs%ef%bc%8c%e5%ae%9e%e7%8e%b0%e6%96%87%e4%bb%b6%e8%af%bb%e5%86%99%e5%a4%8d%e5%88%b6","status":"publish","type":"post","link":"https:\/\/www.chenweikang.top\/?p=283","title":{"rendered":"Node.js-\u6587\u4ef6\u64cd\u4f5c\u6a21\u5757(fs)\uff0c\u5b9e\u73b0\u6587\u4ef6\u8bfb\u5199\u590d\u5236"},"content":{"rendered":"<blockquote><p>Nodejs\u6587\u4ef6 I\/O \u662f\u7531\u7b80\u5355\u5c01\u88c5\u7684\u6807\u51c6 POSIX \u51fd\u6570\u63d0\u4f9b\u7684\u3002 \u901a\u8fc7 require('fs') \u4f7f\u7528\u8be5\u6a21\u5757\u3002 \u6240\u6709\u7684\u65b9\u6cd5\u90fd\u6709\u5f02\u6b65\u548c\u540c\u6b65\u7684\u5f62\u5f0f\u3002\u5f02\u6b65\u5f62\u5f0f\u59cb\u7ec8\u4ee5\u5b8c\u6210\u56de\u8c03\u4f5c\u4e3a\u5b83\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u3002 \u4f20\u7ed9\u5b8c\u6210\u56de\u8c03\u7684\u53c2\u6570\u53d6\u51b3\u4e8e\u5177\u4f53\u65b9\u6cd5\uff0c\u4f46\u7b2c\u4e00\u4e2a\u53c2\u6570\u603b\u662f\u7559\u7ed9\u5f02\u5e38\u3002 \u5982\u679c\u64cd\u4f5c\u6210\u529f\u5b8c\u6210\uff0c\u5219\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f1a\u662f null \u6216 undefined\u3002\u5f53\u4f7f\u7528\u540c\u6b65\u5f62\u5f0f\u65f6\uff0c\u4efb\u4f55\u5f02\u5e38\u90fd\u4f1a\u88ab\u7acb\u5373\u629b\u51fa\u3002 \u53ef\u4ee5\u4f7f\u7528 try\/catch \u6765\u5904\u7406\u5f02\u5e38\uff0c\u6216\u8ba9\u5b83\u4eec\u5f80\u4e0a\u5192\u6ce1\u3002<\/p><\/blockquote>\n<h2>\u4e00\u3001\u4f7f\u7528 createReadStream\u548ccreateWriteStream\u5b9e\u73b0\u6587\u4ef6\u590d\u5236<\/h2>\n<h3>\u6587\u4ef6\u5728\u8bfb\u53d6\u548c\u5199\u5165\u7684\u65f6\u5019\u7531\u4e8e\u4e0d\u662f\u540c\u6b65\u6267\u884c\uff0c\u56e0\u6b64\u5fc5\u987b\u89e3\u51b3\u6587\u4ef6\u8bfb\u53d6\u7684\u4e00\u81f4\u6027\uff0c\u4f7f\u7528data\u4e8b\u4ef6\u548cdrain\u4e8b\u4ef6\u76d1\u542c\u6587\u4ef6\u8bfb\u5199\u60c5\u51b5 \uff0c\u5e76\u5bf9\u6587\u4ef6\u8bfb\u5199\u8fdb\u884c\u6682\u505c\u6216\u6062\u590d\u7b49\u63a7\u5236<\/h3>\n<p>\u65b0\u5efafilecopy.js\u6587\u4ef6\uff0c\u590d\u5236\u5982\u4e0b\u4ee3\u7801\uff0c\u5e76\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u5efa\u4e00\u4e2atest.zip\u6587\u4ef6<\/p>\n<pre>\/\/filecopy.js\r\nvar fs = require(\"fs\"); \r\n\r\nfunction testFileCopy() {\r\n    var readStream = fs.createReadStream(\"test.zip\"); \/\/\u8bfb\u53d6\u6587\u4ef6\u6d41\r\n    var writeStream = fs.createWriteStream(\"test_copy.zip\"); \/\/\u8981\u5199\u5165\u7684\u76ee\u6807\u6587\u4ef6\r\n    \/\/\u8bfb\u53d6\u6570\u636e \u76d1\u542c\u4e8b\u4ef6\r\n    var totalSize = 0;\r\n    readStream.on(\"data\" ,function (chunk) {\r\n        var buffer_good = writeStream.write(chunk);\r\n        totalSize += chunk.length;\r\n        if(!buffer_good){\r\n            readStream.pause(); \/\/\u6682\u505c\u5199\u5165\r\n            console.log(\"\u6682\u505c\u8bfb\u53d6\uff01\u603b\u5927\u5c0f\uff1a\"+totalSize);\r\n        }\r\n    });\r\n    writeStream.on(\"drain\",function () {\r\n        readStream.resume(); \/\/\u6062\u590d\u8bfb\u53d6\r\n        console.log(\"\u6062\u590d\u8bfb\u53d6\uff01\");\r\n    });\r\n\r\n}\r\n\r\ntestFileCopy(); \/\/\u8fd0\u884c\u8be5\u65b9\u6cd5 \u5f53\u524d\u76ee\u5f55\u751f\u6210test_copy.zip\u6587\u4ef6<\/pre>\n<h2>\u4e8c\u3001\u4f7f\u7528pipe\u7ba1\u9053\u65b9\u6cd5\u590d\u5236\u6587\u4ef6<\/h2>\n<pre>\/\/\u4f7f\u7528nodejs\u81ea\u5e26\u7684pipe\u65b9\u6cd5 \u8be5\u65b9\u6cd5\u5b9e\u73b0\u539f\u7406\u540c\u4e0a\r\nfunction testPipe() {\r\n    var readStream = fs.createReadStream(\"test.zip\"); \/\/\u8bfb\u53d6\u6587\u4ef6\u6d41\r\n    var writeStream = fs.createWriteStream(\"test_pipe_copy.zip\");\r\n    readStream.pipe(writeStream);\r\n}\r\n\r\ntestPipe(); \/\/\u8fd0\u884c\u8be5\u65b9\u6cd5 \u5f53\u524d\u76ee\u5f55\u751f\u6210test_pipe_copy.zip\u6587\u4ef6 \u662f\u4e0d\u662f\u8d85\u7ea7\u7b80\u5355<\/pre>\n<p>\u4f7f\u7528createWriteStream\u65b0\u5efa\u6587\u4ef6<\/p>\n<pre>\/\/write\u65b9\u6cd5\r\nvar writeStream = fs.createWriteStream(\"test.txt\");\r\nwriteStream.write(\"\u6211\u662f\u65b0\u751f\u6210\u7684\u6587\u4ef6\uff01\"); \/\/\u8fd0\u884c\u540e\u5f53\u524d\u76ee\u5f55\u751f\u6210\u6587\u4ef6test.txt \u5185\u5bb9\u4e3a \u6211\u662f\u65b0\u751f\u6210\u7684\u6587\u4ef6\uff01\r\n\r\n<\/pre>\n<h2>\u4e09\u3001\u8bfb\u53d6\u6587\u4ef6(readFile)\u3001\u5199\u5165\u6587\u4ef6(writeFile)<\/h2>\n<h3>readFile\uff0cwriteFile\u5747\u4e3a\u5f02\u6b65\u51fd\u6570, readFile\u8bfb\u53d6\u6587\u4ef6\u4e3a\u4e8c\u8fdb\u5236\u6d41\uff0c\u53ef\u76f4\u63a5response\u8f93\u51fa<\/h3>\n<pre>\/\/\u4f7f\u7528readFile,writeFile \u8bfb\u5199\u6587\u4ef6\r\nfunction testReadFile(){\r\n    fs.readFile(\"test.txt\" , function (err, data) {\r\n        console.log(data.length);\r\n        console.log(data);\r\n        fs.writeFile(\"test_copy.txt\", data ); \/\/\u5c06\u4e8c\u8fdb\u5236\u6d41\u5199\u5165\u5230test_copy.txt\u4e2d\r\n    });\r\n}\r\ntestReadFile(); \r\n\/\/\u8fd0\u884c\u7ed3\u679c(\u8be5\u76ee\u5f55\u751f\u6210test_copy.txt)\uff1a\r\n27\r\n&lt;Buffer e6 88 91 e6 98 af e6 96 b0 e7 94 9f e6 88 90 e7 9a 84 e6 96 87 e4 bb b6 ef bc 81&gt;<\/pre>\n<h3>\u56db\u3001\u603b\u7ed3<br \/>\nfs\u6a21\u5757\u5c01\u88c5\u4e86\u5927\u91cf\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5982 \u6587\u4ef6\u6743\u9650\u7684\u64cd\u4f5c\uff0c\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\u7684\u64cd\u4f5c\uff0c\u521b\u5efa\u76ee\u5f55\u3001\u5224\u65ad\u6587\u4ef6\u7b49\u7b49<br \/>\n\u5173\u4e8e\u6587\u4ef6\u64cd\u4f5c\u53c2\u8003API\uff1a<a href=\"http:\/\/nodejs.cn\/api\/fs.html\" target=\"_blank\">http:\/\/nodejs.cn\/api\/fs.html<\/a><\/h3>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.chenweikang.top\">\u5de6\u624b\u4ee3\u7801\u53f3\u624b\u8bd7<\/a> &raquo; <a href=\"https:\/\/www.chenweikang.top\/?p=283\">Node.js-\u6587\u4ef6\u64cd\u4f5c\u6a21\u5757(fs)\uff0c\u5b9e\u73b0\u6587\u4ef6\u8bfb\u5199\u590d\u5236<\/a><\/p><div class=\"__youshang\">\r\n            <div id=\"__youshang_popup\" class=\"wechat popup\" style=\"display: none;\">\r\n                <div class=\"head\">~\u8c22\u8c22\u6253\u8d4f~<\/div>\r\n                <div class=\"qrcode\"><div class=\"qrcode-li wechat\" ><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/wexin.png\" \/><\/div><div class=\"qrcode-li alipay\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-pay.png\" \/><\/div><div class=\"qrcode-li hongbao\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-hongbao.png\" \/><\/div><\/div>\r\n                <ul class=\"platform\"><li class=\"icon-wechat active\" data-bg-color=\"#05af4e\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-alipay \" data-bg-color=\"#00a2ea\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-hongbao \" data-bg-color=\"#dd5746\" data-thanks=\"<p>\u626b\u7801\u9886\u7ea2\u5305<\/p><p style='margin-top: 24px;'>\uff08\u4f59\u989d\u5b9d\u652f\u4ed8\u65f6\u53ef\u62b5\u73b0\uff09<\/p>\"><\/li><\/ul>\r\n            <\/div>\r\n            <a href=\"javascript:void(0);\" id=\"__youshang_btn\">\u8d4f<\/a>\r\n        <\/div>","protected":false},"excerpt":{"rendered":"<p>Nodejs\u6587\u4ef6 I\/O \u662f\u7531\u7b80\u5355\u5c01\u88c5\u7684\u6807\u51c6 POSIX \u51fd\u6570\u63d0\u4f9b\u7684\u3002 \u901a\u8fc7 require('fs') \u4f7f\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":269,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,3],"tags":[44],"class_list":["post-283","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js","category-note","tag-node-js"],"_links":{"self":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=283"}],"version-history":[{"count":0,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/283\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/media\/269"}],"wp:attachment":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}