Node.js File system & Streams
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var path = require("path");
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var fs = require("fs");
Node.js File system & Streams
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var Stream = require("stream");
var stream = new Stream();
var duration = 5 * 1000; // 5 seconds
var end = Date.now() + duration;
var interval;
stream.readable = true;
interval = setInterval(function () {
console.log("Emitting a data event");
stream.emit("data", new Buffer("foo"));
if (Date.now() >= end) {
console.log("Emitting an end event");
stream.emit("end");
clearInterval(interval);
}
}, 1000);
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
 crypto streams
 tcp sockets
 child process stdout and stderr
 process.stdin
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var fs = require("fs");
var stream;
stream = fs.createReadStream(__dirname + "/foo.txt");
stream.on("data", function (data) {
var chunk = data.toString();
process.stdout.write(chunk);
});
stream.on("end", function() {
console.log();
});
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var fs = require("fs");
var readStream = fs.createReadStream(__dirname + "/foo.txt");
var writeStream = fs.createWriteStream(__dirname + "/bar.txt");
readStream.pipe(writeStream);
var fs = require("fs");
var zlib = require("zlib");
var gzip = zlib.createGzip();
var input = fs.createReadStream("input.txt");
var output = fs.createWriteStream("input.txt.gz");
input
.pipe(gzip)
.pipe(output);
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
Use-case Class Method(s) to implement
Reading only Readable _read
Writing only Writable _write
Reading and writing Duplex _read, _write
Operate on written data, then read the result Transform _transform, _flush
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var Readable = require('stream').Readable;
var util = require('util');
util.inherits(Counter, Readable);
function Counter(opt) {
Readable.call(this, opt);
this._max = 1000000;
this._index = 1;
}
Counter.prototype._read = function () {
var i = this._index++;
if (i > this._max)
this.push(null);
else {
var str = '' + i;
var buf = new Buffer(str, 'ascii');
this.push(buf);
}
};
Node.js File system & Streams

More Related Content

PPTX
Node js introduction
PPTX
Spring Boot
PPTX
File system node js
PPTX
Node.js Event Emitter
PDF
JavaScript Programming
PPTX
ASP.NET Web API
PPTX
jQuery
Node js introduction
Spring Boot
File system node js
Node.js Event Emitter
JavaScript Programming
ASP.NET Web API
jQuery

What's hot (20)

PPTX
Spring Boot and REST API
PPTX
Learning Svelte
PPTX
Introduction to Node js
PPT
jQuery Ajax
PPTX
SQLite database in android
PPT
Core java concepts
PDF
Understanding react hooks
PPT
JavaScript Tutorial
PPTX
Spring boot
PDF
Nodejs presentation
PDF
Spring Framework - AOP
PPSX
Javascript variables and datatypes
PPTX
Express JS
PDF
Bootstrap
PPTX
Introduction to NodeJS
PPT
PHP - Introduction to File Handling with PHP
PPTX
Reactjs
PPTX
Files in php
PPTX
C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...
PPTX
Express js
Spring Boot and REST API
Learning Svelte
Introduction to Node js
jQuery Ajax
SQLite database in android
Core java concepts
Understanding react hooks
JavaScript Tutorial
Spring boot
Nodejs presentation
Spring Framework - AOP
Javascript variables and datatypes
Express JS
Bootstrap
Introduction to NodeJS
PHP - Introduction to File Handling with PHP
Reactjs
Files in php
C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...
Express js
Ad

Viewers also liked (20)

PPTX
Node.js Socket.IO
PPTX
Node js overview
PPTX
Angular 2.0 forms
PPTX
Angular 2.0 Views
PPTX
Modules and injector
PPTX
Async & Parallel in JavaScript
PPTX
Angular 2.0 Pipes
PPTX
Angular 2.0 Routing and Navigation
PPTX
Http Communication in Angular 2.0
PPTX
Node.js Spplication Scaling
PPTX
Angular 2 NgModule
PPTX
Routing And Navigation
PPTX
Upgrading from Angular 1.x to Angular 2.x
PPTX
Template syntax in Angular 2.0
PPTX
Component lifecycle hooks in Angular 2.0
PPTX
Angular 2.0 Dependency injection
PPTX
Angular 2 - Ahead of-time Compilation
PPTX
Performance Optimization In Angular 2
PPTX
Angular 1.x vs. Angular 2.x
PPTX
Modules in ECMAScript 6.0
Node.js Socket.IO
Node js overview
Angular 2.0 forms
Angular 2.0 Views
Modules and injector
Async & Parallel in JavaScript
Angular 2.0 Pipes
Angular 2.0 Routing and Navigation
Http Communication in Angular 2.0
Node.js Spplication Scaling
Angular 2 NgModule
Routing And Navigation
Upgrading from Angular 1.x to Angular 2.x
Template syntax in Angular 2.0
Component lifecycle hooks in Angular 2.0
Angular 2.0 Dependency injection
Angular 2 - Ahead of-time Compilation
Performance Optimization In Angular 2
Angular 1.x vs. Angular 2.x
Modules in ECMAScript 6.0
Ad

More from Eyal Vardi (11)

PPTX
Why magic
PPTX
Smart Contract
PDF
Rachel's grandmother's recipes
PPTX
Angular 2 Architecture (Bucharest 26/10/2016)
PPTX
Angular 2 Architecture
PPTX
Proxies in ECMAScript 6.0
PPTX
Iterators & Generators in ECMAScript 6.0
PPTX
Symbols in ECMAScript 6.0
PPTX
Objects & Classes in ECMAScript 6.0
PPTX
Scope & Functions in ECMAScript 6.0
PPTX
AngularJS Internal
Why magic
Smart Contract
Rachel's grandmother's recipes
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture
Proxies in ECMAScript 6.0
Iterators & Generators in ECMAScript 6.0
Symbols in ECMAScript 6.0
Objects & Classes in ECMAScript 6.0
Scope & Functions in ECMAScript 6.0
AngularJS Internal

Recently uploaded (20)

PPTX
Blending method and technology for hydrogen.pptx
PPTX
From XAI to XEE through Influence and Provenance.Controlling model fairness o...
PDF
Altius execution marketplace concept.pdf
PPTX
maintenance powerrpoint for adaprive and preventive
PPTX
Information-Technology-in-Human-Society (2).pptx
PDF
Ebook - The Future of AI A Comprehensive Guide.pdf
PDF
Slides World Game (s) Great Redesign Eco Economic Epochs.pdf
PDF
Optimizing bioinformatics applications: a novel approach with human protein d...
PDF
Fitaura: AI & Machine Learning Powered Fitness Tracker
PPTX
From Curiosity to ROI — Cost-Benefit Analysis of Agentic Automation [3/6]
PDF
Child-friendly e-learning for artificial intelligence education in Indonesia:...
PDF
【AI論文解説】高速・高品質な生成を実現するFlow Map Models(Part 1~3)
PDF
Be ready for tomorrow’s needs with a longer-lasting, higher-performing PC
PDF
TrustArc Webinar - Data Minimization in Practice_ Reducing Risk, Enhancing Co...
PPTX
AQUEEL MUSHTAQUE FAKIH COMPUTER CENTER .
PDF
TicketRoot: Event Tech Solutions Deck 2025
PDF
Technical Debt in the AI Coding Era - By Antonio Bianco
PDF
GDG Cloud Southlake #45: Patrick Debois: The Impact of GenAI on Development a...
PDF
Domain-specific knowledge and context in large language models: challenges, c...
PDF
The Digital Engine Room: Unlocking APAC’s Economic and Digital Potential thro...
Blending method and technology for hydrogen.pptx
From XAI to XEE through Influence and Provenance.Controlling model fairness o...
Altius execution marketplace concept.pdf
maintenance powerrpoint for adaprive and preventive
Information-Technology-in-Human-Society (2).pptx
Ebook - The Future of AI A Comprehensive Guide.pdf
Slides World Game (s) Great Redesign Eco Economic Epochs.pdf
Optimizing bioinformatics applications: a novel approach with human protein d...
Fitaura: AI & Machine Learning Powered Fitness Tracker
From Curiosity to ROI — Cost-Benefit Analysis of Agentic Automation [3/6]
Child-friendly e-learning for artificial intelligence education in Indonesia:...
【AI論文解説】高速・高品質な生成を実現するFlow Map Models(Part 1~3)
Be ready for tomorrow’s needs with a longer-lasting, higher-performing PC
TrustArc Webinar - Data Minimization in Practice_ Reducing Risk, Enhancing Co...
AQUEEL MUSHTAQUE FAKIH COMPUTER CENTER .
TicketRoot: Event Tech Solutions Deck 2025
Technical Debt in the AI Coding Era - By Antonio Bianco
GDG Cloud Southlake #45: Patrick Debois: The Impact of GenAI on Development a...
Domain-specific knowledge and context in large language models: challenges, c...
The Digital Engine Room: Unlocking APAC’s Economic and Digital Potential thro...

Node.js File system & Streams

  • 2. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 3. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 4. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 5. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] var path = require("path");
  • 6. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] var fs = require("fs");
  • 8. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 9. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] var Stream = require("stream"); var stream = new Stream(); var duration = 5 * 1000; // 5 seconds var end = Date.now() + duration; var interval; stream.readable = true; interval = setInterval(function () { console.log("Emitting a data event"); stream.emit("data", new Buffer("foo")); if (Date.now() >= end) { console.log("Emitting an end event"); stream.emit("end"); clearInterval(interval); } }, 1000);
  • 10. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 11. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]  crypto streams  tcp sockets  child process stdout and stderr  process.stdin
  • 12. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 13. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] var fs = require("fs"); var stream; stream = fs.createReadStream(__dirname + "/foo.txt"); stream.on("data", function (data) { var chunk = data.toString(); process.stdout.write(chunk); }); stream.on("end", function() { console.log(); });
  • 14. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] var fs = require("fs"); var readStream = fs.createReadStream(__dirname + "/foo.txt"); var writeStream = fs.createWriteStream(__dirname + "/bar.txt"); readStream.pipe(writeStream);
  • 15. var fs = require("fs"); var zlib = require("zlib"); var gzip = zlib.createGzip(); var input = fs.createReadStream("input.txt"); var output = fs.createWriteStream("input.txt.gz"); input .pipe(gzip) .pipe(output);
  • 16. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] Use-case Class Method(s) to implement Reading only Readable _read Writing only Writable _write Reading and writing Duplex _read, _write Operate on written data, then read the result Transform _transform, _flush
  • 17. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] var Readable = require('stream').Readable; var util = require('util'); util.inherits(Counter, Readable); function Counter(opt) { Readable.call(this, opt); this._max = 1000000; this._index = 1; } Counter.prototype._read = function () { var i = this._index++; if (i > this._max) this.push(null); else { var str = '' + i; var buf = new Buffer(str, 'ascii'); this.push(buf); } };