const fs = require('fs');
//createReadStream: read the file
//first argument: location of the file
//second argument: option argument
//encoding: 'utf8' : enabled the readable format
const readStream = fs.createReadStream('./docs/blog3.txt', {encoding: 'utf8'});
//createWriteStream: write to the file
const writeStream = fs.createWriteStream('./docs/blog4.txt');
//on: file readevent listener
//everytime we receive the buffer of data, it performs
//chunk is the buffer of data
//write enabled to write it in the stream
readStream.on('data', (chunk) => {
console.log('----- NEW CHUNK ----');
console.log(chunk/toString());
writeStream.write('\nNEW CHUNK\n');
writeStream.write(chunk);
})
//pipe: pass data from readable to writable stream
//it must from readable one to writable one
readStream.pipe(writeStream);
'Node.js' 카테고리의 다른 글
[Node.js] Check if my Node package has every files I want them to be (0) | 2022.10.04 |
---|---|
Node 16 >> AWS Lambda (0) | 2022.06.18 |
[Node.js] File System (fs) read, write, delete files, and directories (0) | 2022.04.28 |
[Node.js] Streams (0) | 2022.04.23 |
[Node.js] Node.js Child Processes [spawn(), exec(), execFile(), and fork()] (0) | 2022.04.23 |