/** * Created by Administrator on 2016/3/21. */ var fs = require('fs'); var fruitBowl = ['apple', 'orange', 'banana', 'grapes']; function writeFruit(fd){ if (fruitBowl.length){ var fruit = fruitBowl.pop() + " "; // fs.write(fd, buffer, offset, length[, position], callback); // fs.write(fd, string[, position[, encoding]], callback); // fs.write = function(fd, buffer, offset, length, position, callback) fs.write(fd, fruit, null, null, function(err, bytes){ if (err){ console.log("File Write Failed."); } else { console.log("Wrote: %s %dbytes", fruit, bytes); writeFruit(fd); } }); } else { fs.close(fd); ayncRead(); } } fs.open('data/fruit.txt', 'w', function(err, fd){ writeFruit(fd); }); function ayncRead() { var fs = require('fs'); function readFruit(fd, fruits){ var buf = new Buffer(5); buf.fill(); //fs.read = function(fd, buffer, offset, length, position, callback) fs.read(fd, buf, 0, 5, null, function(err, bytes, data){ if ( bytes > 0) { console.log("read %dbytes", bytes); fruits += data; readFruit(fd, fruits); } else { fs.close(fd); console.log ("Fruits: %s", fruits); } }); } fs.open('data/fruit.txt', 'r', function(err, fd){ readFruit(fd, ""); }); }
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe asyncReadWrite.js Wrote: grapes 7bytes Wrote: banana 7bytes Wrote: orange 7bytes Wrote: apple 6bytes read 5bytes read 5bytes read 5bytes read 5bytes read 5bytes read 2bytes Fruits: grapes banana orange apple Process finished with exit code 0
4.流式读写
/** * Created by Administrator on 2016/3/21. */ var fs = require('fs'); var grains = ['wheat', 'rice', 'oats']; var options = { encoding: 'utf8', flag: 'w' }; //从下面的系统api可以看到 createWriteStream 就是创建了一个writable流 //fs.createWriteStream = function(path, options) { // return new WriteStream(path, options); //}; // //util.inherits(WriteStream, Writable); //fs.WriteStream = WriteStream; //function WriteStream(path, options) var fileWriteStream = fs.createWriteStream("data/grains.txt", options); fileWriteStream.on("close", function(){ console.log("File Closed."); //流式读 streamRead(); }); while (grains.length){ var data = grains.pop() + " "; fileWriteStream.write(data); console.log("Wrote: %s", data); } fileWriteStream.end(); //流式读 function streamRead() { var fs = require('fs'); var options = { encoding: 'utf8', flag: 'r' }; //fs.createReadStream = function(path, options) { // return new ReadStream(path, options); //}; // //util.inherits(ReadStream, Readable); //fs.ReadStream = ReadStream; // //function ReadStream(path, options) //createReadStream 就是创建了一个readable流 var fileReadStream = fs.createReadStream("data/grains.txt", options); fileReadStream.on('data', function(chunk) { console.log('Grains: %s', chunk); console.log('Read %d bytes of data.', chunk.length); }); fileReadStream.on("close", function(){ console.log("File Closed."); }); }
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe StreamReadWrite.js Wrote: oats Wrote: rice Wrote: wheat File Closed. Grains: oats rice wheat Read 16 bytes of data. File Closed. Process finished with exit code 0
个人觉得像这些api用一用感受一下就ok了,遇到了会用就行了。
您可能感兴趣的文章: