nodejs 檔案操作__js

來源:互聯網
上載者:User

引入檔案系統模組:

var fs = require("fs");

使用readFile和writeFile進行操作:

讀取檔案內容:



exports.t1=function(){console.log("開始讀取檔案。");fs.readFile("./hello.js",function(err,data){if(err){return console.log(err);}console.log(data.toString());});//非同步處理var data=fs.readFileSync("./hello.js");console.log(data.toString());console.log("檔案讀取結束。");//同步處理}
複製檔案內容:
exports.copy1=function(){fs.readFile("./hello.js",function(err,data){if(err){return console.log(err);}fs.writeFile("./hello_c.js",data);});}
使用open,read,write進行操作:
exports.t2 = function() {var buf = new Buffer(1024);fs.open("./hello.js", "r", function(err, fd) {if(err) {return console.log(err);}fs.read(fd, buf, 0, buf.length, 0, function(err, bytes) {if(err) {return console.log(err);}console.log("一共讀取了" + bytes + "位元組");console.log(buf.slice(0, bytes).toString());});fs.close(fd, function(err) {if(err) {return console.log(err);}});});}
exports.copy2 = function() {var buf = new Buffer(1024);fs.open("./hello.js", "r", function(err, rd) {if(err) {return console.log(err);}fs.read(rd, buf, 0, buf.length, 0, function(err, bytes) {if(err) {return console.log(err);}fs.open("./hello_c.js", "w", function(err, wd) {if(err) {return console.log(err);}fs.write(wd,buf,0,bytes,function(err,written,buffer){fs.close(wd, function(err) {if(err) {return console.log(err);}});console.log(written);if(err) {return console.log(err);}});});});fs.close(rd, function(err) {if(err) {return console.log(err);}});});}
使用事件迴圈進行操作:
exports.copy3=function(){var buf = new Buffer(1024);var eventEmitter=new event.EventEmitter();eventEmitter.on("copy.star",function(){fs.open('./hello.js','r',function(err,rd){if(err){return console.log(err);}eventEmitter.emit("copy.read",rd);});}).on("copy.read",function(rd){fs.read(rd,buf,0,buf.length,0,function(err,bytes){fs.close(rd,function(err){if(err){return console.log(err);}});if(err){return console.log(err);}eventEmitter.emit("copy.write",bytes);});}).on("copy.write",function(bytes){fs.open("./hello_c.js","w",function(err,wd){if(err){return console.log(err);}fs.write(wd,buf,0,bytes,function(err,writen,buffer){fs.close(wd,function(err){if(err){return console.log(err);}});if(err){return console.log(err);}});});}).emit("copy.star");}
當複製內容超過規定的數值的時候,就會出錯,所以可以使用createReadStream,createWriteStream方法。

exports.t3=function(){var read=fs.createReadStream("./hello.js");read.on("data",function(data){console.log(data.toString());});read.on("end",function(){console.log("檔案讀取結束。");})}
exports.copy4=function(){var read=fs.createReadStream("./hello.js");var write=fs.createWriteStream("./hello_c.js");read.on("data",function(data){write.write(data);});read.on("end",function(){write.end()});read.on("error",function(err){ console.log(err);})}
最簡單的方法,使用管道:
exports.copy5=function(){var read=fs.createReadStream("./hello.js");var write=fs.createWriteStream("./hello_c.js");read.pipe(write);}
nodejs 的檔案系統API






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.