// Import 'fs' module
const fs = require('fs');
// Create read and write streams
const readStream = fs.createReadStream('input.txt');
const writeStream = fs.createWriteStream('output.txt');
// Pipe data from read stream to write stream for file copy
readStream.pipe(writeStream);
code
File Stream Copy using 'fs' module
This Node.js code uses the 'fs' module to create a read stream (readStream) from 'input.txt' and a write stream (writeStream) to 'output.txt'. The pipe() method is then used to copy data from the read stream to the write stream, effectively copying the contents of 'input.txt' to 'output.txt'. The comments provide explanations for each part of the code.
Node.js
File Stream
fs module
Copy
Pipe