- [Instructor] In the last lesson, we took a look
at creating readable file streams.
We've also been using writable streams too.
The process.stdout object is a writeable stream.
I'm going to go into the start folder and create a New File.
And, I will call this file, writeStream.
So, process.stdout is a writeable stream,
which means it has a write method.
And, we can use the write method
to write data to a writeable stream.
So, if I wrote hello
word/n we can actually take this file
and run it in the TERMINAL and type node writeStream
and we can see the data hello world
has been written to the TERMINAL.
So, we can actually use the exact same interface
to write a file.
First thing I'm going to do is include the fs module
by requiring it.
And, I can create writeable file streams
using the fs.create writeStream method.
And, the first argument here is
where we're going to write the file.
So, I'll do this in the assets folder /myFile.txt
and again the second argument
we're going to create a text writeable stream,
so we'll say UTF-8 for the encoding.
So, now we have a writeable stream
and we can use this writeable file stream
just like process standard output.
The difference is we are now writing output to a file.
So, now if I go ahead and save this and open up the TERMINAL
and type node writeStream,
we don't see any output in the TERMINAL.
That's because we're no longer
writing it using process.stdout.
Instead, we've created a file stream.
So, now we have myfile.txt and that's
where the hello world was written.
Readable streams are made to work with writeable streams.
So, if I come in here to this writeStream,
I'm going to go ahead and delete these last two lines of code
and let's go ahead and use the process.stdin readable stream
and we'll listen for data events from the TERMINAL.
And, when we have a data event,
what we can do is use the writeStream to write
that data event to a file.
So, readable streams are made
to work with writeable streams.
Let's go ahead and run this in the TERMINAL.
So, this time we're listening for some data.
If I type, I am Alex This is Node js
and go ahead and take a look
at that text file, you'll notice that
what I am writing in the terminal is in fact getting written
to this text file as I write it.
Let's go ahead and further exemplify this
by creating a readStream using fs.createReadStream
and we can go ahead and read that lorem ipsum file,
so it's the exact readStream
that we created in the last lesson.
So, instead of using process.stdin, I can go ahead
and just read bits of data from this readStream
and then write 'em to my new file or my writeStream.
So, let's go ahead and do this.
I'll come back out to the TERIMNAL.
I'm going to stop this current app from running
by hitting Control + C and typing clear
to get rid of the extra output in the TERMINAL.
And now, when I run node writeStream
we see nothing happened, but what we just did
was actually copy the file.
So, if I look at myFile.txt, bit by bit by bit, we were able
to copy the lorem ipsum file into my file.
So, because readable streams are made to work
with writeable streams, they even have a lot
of methods that help you compose them.
For instance, I can take the process.stdin,
which is a readable stream and pipe it to our writeStream
and it sends it to the writeStream which will write it
to the myFile.txt file.
So, let's go ahead and run this.
I'll type Hello my name is Alex and then we can go ahead
and take a look at myFile.txt within our assets folder
and we can see that that data's getting written directly
to the file.
So, this works with all sorts of streams.
If I wanted to take the readStream,
which is our lorem ipsum readStream
and then pipe it to the writeStream,
which is a reference to the myFile.txt, we can do that too.
So, I'll come out here to the TERMINAL
and run this one last time.
Oops, and we notice that we are calling readStream.stdin.
We actually, that's the readStream,
so we can delete the stdin and try it again.
Let me save this file and come back out here.
Clear the TERIMINAL and run it.
So, nothing happens except we piped from the lorem ipsum
mark down document to the writeStream which is my text
and we essentially copied the file.
The writeable stream is the counterpart
to the readable stream.
And, just like readable streams,
writeable streams are everywhere in node js.