代码:
//数据的分批写入
let fs = require('fs');
let path = require('path');
//1.拼接读取的路径
let str = path.join(__dirname, "it666.txt");
//2.创建一个读取流
let writeStream = fs.createWriteStream(str, {
encoding: 'utf8',
highWaterMark: 1,
})
//3.添加事件监听:
writeStream.on("open", function () {
console.log("表示数据流和文件建立关系成功");
});
writeStream.on("error", function () {
console.log("表示数据流和无文件建立关系失败");
});
writeStream.on("data", function (data) {
console.log("表示通过读取流从文件中读取到了数据", data);
});
writeStream.on("close", function () {
console.log("表示数据流断开了和文件的关系,并且数据已经读取完毕");
});
//要写入的字符串
let data = "www.it666.com";
let index = 0;
let timeId = setInterval(function () {
let ch = data[index];
index++;
writeStream.write(ch);
console.log("本次写入了 ", ch);
if (index === data.length) {
clearInterval(timeId);
}
}, 100)
run:
版权声明:本文为ice_stone_kai原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。