1.

Explain piping the streams?

Answer»

Piping is 

  • a mechanism where output of one stream is provided as INPUT to ANOTHER stream 
  • normally used to get data from one stream and pass the data to another stream

No limit on piping operations.

Example: Create a text file dataSinput.txt with the following content.

  • Monday is first day of WEEK
  • Tuesday is second day of week
  • Wednesday is third day of week

After executing the following code you can view the contents in the outputfile.

var fs = require("fs"); //import fs module //creating a readstream to READ our inputdatafile dataSinput.txt var readStream = fs.createReadStream("F://dataSinput.txt"); //creating a writestream(initially empty) which is destination for transferred data  var writeStream = fs.createWriteStream("F://dataSoutput.txt"); //Use Pipe command to transfer from readstream to writestream.  //Pipe command takes all data from readstream and PUSHES it to writestream  readStream.pipe(writeStream);

Output in dataSoutput.txt can be seen as

  • Monday is first day of week
  • Tuesday is second day of week
  • Wednesday is third day of week


Discussion

No Comment Found