1. Read fs.createReadStream as a stream when there is too much file content

1) How to write?

const fs = require("fs"); Var readStream = fs.createreadStream ("./input.txt"); var count = 0; var str = ""; readStream.on("data", (data) => { str += data; Count++; }); Readstream. on("end", () => {console.log(STR); console.log(count); }); Readstream. on("error", (err) => {console.log(err); });Copy the code

You can see the result after executing Node app.js

2、以流的方式 写入文件 fs.createWriteStream

1) How to write?

Const fs = require("fs"); var str = ""; for (var i = 0; i < 300; I++) {STR += "I got the data from the database and I need to save it \n"; } var writeStream = fs.createWritestream ("./output.txt"); writeStream.write(str); // mark the end of the file writestream.end (); Writestream. on("finish", () => {console.log(" Write complete "); });Copy the code

3. Pipeline flow uses many very important concepts!

1) The solution scenario is to put a file or image in another folder (usually for large things)

Let me give you an example to illustrate the current situation

Const fs = require("fs"); Var readStream = fs.createreadStream ("./app.js"); Var writeStream = fs.createWritestream ("./data/app-copy.js"); Readstream. pipe(writeStream);Copy the code