Stream has four types of streams:
readable -readable operation.
writable -writable operation.
Duplex -readable writable operation.
Transform -The operation is written to the data and then the result is read.
All Stream objects are instances of Eventemitter. The usual events are:
data-triggered when there is a read.
End -triggers when there is no more data to read.
Error-triggered when a fault occurs during receive and write.
finish -triggers when all data has been written to the underlying system.
Reading data from the stream
Create a Input.txt file with the following content:
Beginner's Tutorial official website address: www.runoob.com
Create the Main.js file with the following code:
varFS = require ("FS");vardata = ';//to create a readable streamvarReaderstream = Fs.createreadstream (' input.txt '));//set the encoding to UTF8. Readerstream.setencoding (' UTF8 ');//Process Stream Events--data, end, and errorReaderstream.on (' Data ',function(chunk) {data+=Chunk;}); Readerstream.on (' End ',function() {console.log (data);}); Readerstream.on (' Error ',function(Err) {console.log (err.stack);}); Console.log ("Program execution complete");
Write stream
varFS = require ("FS");vardata = ' Novice Tutorial ' website address: www.runoob.com ';//Create a stream that can be written to the file Output.txtvarWriterstream = Fs.createwritestream (' output.txt '));//writing data using UTF8 encodingWriterstream.write (data, ' UTF8 '));//Mark file Endwriterstream.end ();//Process Stream Events--data, end, and errorWriterstream.on (' Finish ',function() {Console.log ("Write complete. ");}); Writerstream.on (' Error ',function(Err) {console.log (err.stack);}); Console.log ("Program execution complete");
Pipe flow
The pipeline provides a mechanism for outputting a stream to the input stream. Typically we use it to fetch data from one stream and pass it to another stream . If you write the contents of Input.txt to Output.txt
var fs = require ("FS"); // Create a readable stream var readerstream = Fs.createreadstream (' input.txt '); // Create a writable stream var writerstream = Fs.createwritestream (' output.txt '); // Pipe Read and write operations // reads the contents of the Input.txt file and writes the contents to the Output.txt file readerstream.pipe (Writerstream); Console.log ("Completion of program execution");
Chained flow
A chain is a mechanism that flows through the connection output to another stream and creates multiple pairs of flow operation chains. Chained streams are typically used for pipeline operations.
Next we are using pipelines and chains to compress and decompress files.
Create the Compress.js file with the following code:
var fs = require ("FS" var zlib = require (' zlib ' // compressed Input.txt file is input.txt.gz fs.createreadstream (' input.txt ' "file compression complete.) ");
var fs = require ("FS"); var zlib = require (' zlib '); // Unzip the input.txt.gz file to Input.txtfs.createreadstream (' input.txt.gz ') . Pipe (Zlib.creategunzip ()) . Pipe (Fs.createwritestream (' input.txt ')); Console.log ("file decompression completed. ");
"node. js" Stream (Stream)