Node.js 写入文件流的缓存问题探讨

来源:互联网 发布:nginx中文 编辑:程序博客网 时间:2024/05/24 08:33

客户端学得差不多了,为了打通客户端和服务端的任督二脉, 现在小白在看node.js.
关于写文件流的操作,想知道缓存最多能一次性存入多少字节。

下面是代码:

var count = 0; //计数器记录可以写多少个字符。var writeable = fs.createWriteStream('./testInput.txt'); //创建写文件流的对象。for (let i = 0; i < 10000; i++) {    count++;    let flag = writeable.write(i.toString()); //循环写字符,如果缓存区满,flag则为false;    if (!flag) {console.log(count); break;} //触发flag,并且打印出写了多少字符}writeable.end(function () {    console.log('totally %d bytes', writeable.bytesWritten);}); //监听是否写文件结束,如果结束,则输出总共写了多少字节writeable.on('drain', function () {    console.log('data in buffer is all out!');}); //缓存区已满触发事件

输出结果:

4374totally 16386 bytes
原创粉丝点击