- [Instructor] We can also use the FS Module
to read the contents of a file.
Let's go into our start folder and
create a new file called readfile.js.
And within the readfile.js we're also
going to use the FS module so the first thing
that I'm going to do is require that module.
And then I'm going to go ahead and read
the text that's located in one of our assets.
So I'm going to read the text from
the readme.md file, there's just
some text as we can see here, so
I'm going to go back to read file and
I'm going to fs.readFileSync, so
we're going to do this first synchronously
which means that this is going to be blocking.
We're going to wait until we've read the contents
of the entire file before executing any more code
so we'll read it in assets/Readme.md.
The second argument that I want to supply
is the encoding of a file, since this is
a text file, we're going to read this as UTF-8.
So now I can go ahead and console.log text
then I can open up our terminal and
we will run this module so I will node
readFile and in doing so you can see
we just logged the contents of our
text file to the terminal, so we
read that text into that variable name text.
Now this is readFileSync so this is block encode.
We can also use the readFile function
which is the asynchronous way of reading files.
That means that once the contents of the file
have been read, we can pass those contents
to a callback function, so again
the first variable of the callback function is
err, that will be null if there aren't any errors.
And then the second argument is the
text that we're actually reading
since we're reading this file as UTF-8.
Since the text is going to be passed to
this callback once the file has been read
that means that I have to move the
console log to within this callback function.
And I'll also add another console
log, file contents read, great.
So let's go ahead and go down to
our terminal, I'm going to clear the screen
by typing clear, if you're using DOS
you can type CLS and then we will
node readFile and we see the same results.
Now this is how we read a text file.
We can also read binary files.
So instead of reading the Readme file
using this function, I'm going to go into
my assets and I'm going to read the Alex.jpg.
Alex.jpg is an image, this file
isn't encoded as text so I'm going to
get rid of the UTF-8 and instead of
calling this text here, I'm going to
call it the actual image because
we're going to be reading the binary file.
All right, so the biggest difference in
reading binary files is we do not
supply the encoding argument, so I
got rid of UTF-8 and let's go ahead
and take a look at the contents of this file.
Node readFile, and as we can see
the contents of that file have been
read into a buffer and that's our binary.
So that is our actual image file that is Alex.jpg.
The last thing that we want to do is
just make sure we handle errors appropriately so
if any errors occur, we'll go ahead and
console.log an error has occurred.
And we'll go ahead and put the error message out there.
Great, right before we call process.exit.
Excellent, so this shows us that we have
a little flexibility around handling errors.
In this case we're just going to log
the error message and then exit the process.
So since I've added a little bit of
error handling, let's go ahead and test it out.
Let's try and read a file called
Alex.jpeg, it doesn't exist, so I'm going to
save this and node readFile and we can
see that an error has occurred.
There's no such file or directory as Alex.jpeg.
So again, we can read text or binary files.
If we want to read a binary file
we do not supply the encoding.
And we read the contents of that binary file
into a node JS type that we call the buffer.