createWriteStream write stream
Based on fs module
let fs=require("fs");
createWriteStream creates a write stream
We first create a 2.txt file to be written
let ws=fs.createWriteStream('./2.txt');
If the file is not stored, it will be created. If it exists, it will be Clear the file class content first, and then write the file
The first parameter, write path
The second highest water mark The default is 16K
The default is Write in utf8 format
let fs=require("fs"); //文件不存储会创建 如果存在会先清空文件类容,再将文件写入//第一个参数,写入路径// 第二个最高水位线 默认16K// 默认是utf8格式写入let ws=fs.createWriteStream('./2.txt'); // write是异步方法,有返回值,每次调用write方法会返回布尔值//write里面只能是字符串或bufferlet flag=ws.write('1'); //监控内存里面全部写完了,恢复读取,才会调用此方法ws.on('drain',function () { console.log("内存干了"); } ); ws.end() //结束,如果调用end,会强制将内存中的内容全部写入,然后关闭文件// 返回值是true表示能继续写入,如果为false就不要继续写入//res.write() res是一个可写流//假如文件大小1G大,每次读取64K,每次写16K,写不完的剩下的放内存,已经写不下时候,先暂停不要读取了//等我把内存空间的写完成了,和当前写流中的内容全部写入后,再继续读取
The above is the detailed content of How to write a stream using node.js createWriteStream. For more information, please follow other related articles on the PHP Chinese website!