Node.js Stream Data to Text File: A Comprehensive Guide
To stream data to a text file using Node.js, you can use the 'fs' module. Here's an example:
const fs = require('fs');
// create a write stream to a text file
const writeStream = fs.createWriteStream('output.txt');
// start streaming data
while (true) {
const { done, value } = await reader.read();
if (done) break;
// convert data to a string and write it to the file
const text = value.toString();
writeStream.write(text);
}
// close the write stream
writeStream.end();
This code creates a write stream to a file called 'output.txt', and then processes the streamed data, converting it to a string and writing it to the file. Once all the data has been processed, the write stream is closed.
原文地址: https://www.cveoy.top/t/topic/mluy 著作权归作者所有。请勿转载和采集!