DEV Community

Cover image for Reading, Writing and Pipelining Streams in Node.js
Mrityunjay-Palled
Mrityunjay-Palled

Posted on

Reading, Writing and Pipelining Streams in Node.js

Streams are the objects that allow you to read data from a source or write data to a destination in a continuous manner.

Reading from a stream:

First create a text file named "myFile.txt" with the following content

The greater damage for most of us is not that our aim is too high and we miss it, but that it it too low and we reach it.

create a js file named index.js with the following code

//importing file system module
let fs = require("fs");
let data = "";
//creating a redable stream
let rs = fs.createReadStream("myFile.txt");
// Get the data as utf8 strings.
// If an encoding is not set, Buffer objects will be received.
rs.setEncoding("UTF8");
// Readable streams emit 'data' events once a listener is added.
rs.on("data", function (chunk) {
  data += chunk;
});
//end event specifies that body has been read completely 
rs.on("end", function () {
  console.log(data);
});
//error occured 
rs.on("error", function (err) {
  console.log(err.stack);
});

console.log("Program Ended");
Enter fullscreen mode Exit fullscreen mode

we have two events for readable stream one is "data" another is "end"

data: Emitted when data is available
end: Emitted when there is no data

Run the code by writing the following command:

node index.js
Enter fullscreen mode Exit fullscreen mode

Output of the above code is as shown below:

Program Ended
The greater damage for most of us is not that our aim is too high and we miss it, but that it it too low and we reach it.

Enter fullscreen mode Exit fullscreen mode

Writing to a stream:

create a js file named index.js with the following code

//import file system module
let fs = require("fs");
//data to be written to a stream
let data = `I hated every minute of training, but I said, Don't quit. Suffer now and live the rest of your life as a champion.`;
// creating a writable stream
let ws = fs.createWriteStream('output.txt');
//using UTF8 encoding
ws.write(data,'UTF8');
// indicating end of file
ws.end();
// handling finish event
ws.on('finish', function() {
   console.log("Write completed.");
});
//handling error event
ws.on('error', function(err) {
   console.log(err.stack);
});

console.log("Program Ended");
Enter fullscreen mode Exit fullscreen mode

In the above code, we are creating a write stream using the createWriteStream method. We are then using the write method to write data to our output file.Then, we can call the end function to mark the end of the file. "finish" event is emitted when writing data to a stream is completed.

Run the code by writing the following command:

node index.js
Enter fullscreen mode Exit fullscreen mode

Output of the above code is as shown below:

Program Ended
Write completed.
Enter fullscreen mode Exit fullscreen mode

Once the above program is executed completely, you can notice that a file named "output.txt" will be created in your file tree with the following content:

I hated every minute of training, but I said, Don't quit. Suffer now and live the rest of your life as a champion.

Pipelining the streams:

Pipelining means providing the output of one stream as an input to another stream. Now let's take a look at pipelining

create a js file named index.js with the following code

//importing a file system module
let fs = require("fs");
// creating a redable stream
var rs = fs.createReadStream("myFile.txt");
// creating a writable stream
var ws = fs.createWriteStream("output.txt");
//pipeling the read and write
//reading from myFile.txt and writing it to output.txt
rs.pipe(ws);
console.log("Program Ended");
Enter fullscreen mode Exit fullscreen mode

Run the code by writing the following command:

node index.js
Enter fullscreen mode Exit fullscreen mode

Output of the above code is as shown below:

Program Ended
Enter fullscreen mode Exit fullscreen mode

Once the above program is executed completely, you can notice that a file named "output.txt" will be created in your file tree with the same content that is present in the "myFile.txt".

Top comments (0)