Streams - JavaScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now i have a very large file that i want to read so i'm going to try and use the file system to read it fs dot read file sync put in the path dot slash 42 file.txt bar data is equal to and i'm going to try and log out that data okay so i try to run node 42 streams.js okay i'm going to get an error error file system file too large file size is greater than two gigabytes everyone's computer is a little different some of you can't open files larger than 500 megabytes all right i have a 64-bit computer so mine can open two gigabytes maybe yours can only open one gigabyte either way i need another way to read the data in this file right that's why we have streams so instead of trying to read the file synchronously i'm going to create a stream var stream is equal to fs.create read stream all right that's going to take in the file path dot slash 42 file.txt right then i want to listen for stream dot on data and that's going to take in a callback function with the data and i can console.log the data and then i'm going to just use process.exit exit code 0 which means a success but i'm going to exit after the first data stream comes in right so i'm going to run the file now all right you see we get out the first buffer all right and see if i try to convert that to a string all right there you go you see i'm getting um some text from a book logged out right so this is a pretty big book if i try to log out all of this data this will continue running you can see that some of the uh the encoding isn't proper i'm just going to cancel that to show you that you can create a read stream to read large amounts of data like this if you want you can create a listener for on stream on open just console.log stream opened stream dot on close stream closed and also you want to make sure you handle any errors so stream dot on error function that'll return an error okay and if i wanted to create a right stream so that i read the stream from one file and then i write it to a different file i can create a write stream it's fs dot create write stream and let's say i write it to 42 file2.txt then when the data comes in i can say w stream dot right okay and that's going to take in the chunk so i'm going to write the data before i put it into a string i'm actually going to comment out data.2string and i'm going to run the program and see if it takes a long time to write two gigabytes of information here we go node 42 streams stream opened stream closed not bad let's check the file all right and we successfully created the new file and it's too big to open here so if i check both the files here right you can see that we did successfully make a copy of the file
Info
Channel: Nick Bisignano
Views: 298
Rating: undefined out of 5
Keywords:
Id: Vpqx7HOUQao
Channel Id: undefined
Length: 4min 27sec (267 seconds)
Published: Tue Dec 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.