const { pipeline } = require('stream');
let readStream = fs.createReadStream("./myDataInput.txt");
let writeStream = fs.createWriteStream("./myDataOutput.txt");
pipeline(readStream, writeStream, error => {
if (error) {
console.error(error);
} else {
console.info("Pipeline Successful")
}
});
const fs = require('fs');
const { pipeline, Transform } = require('stream');
const readStream = fs.createReadStream("text.txt", {
flags: 'r',
encoding: 'utf-8',
autoClose: true,
});
const writeStream = fs.createWriteStream("text2.txt", {
flags: 'w',
encoding: 'utf-8',
autoClose: true,
});
const transformStream = new Transform({
//Transform data to filter for even numbers
transform(chunk, enc, cb) {
const data = chunk.toString().split(" ").filter(value => !(parseInt(value) & 1));
cb(null, data.join(" "));
}
});
pipeline(readStream, transformStream, writeStream, (err) => {
if(err)
console.error(err);
else
console.error("success");
//writeStream has written only even numbers from readStream
})