10.1 Reading from Files - Video Tutorials & Practice Problems
Video duration:
9m
Play a video:
<v Teacher>In this first section,</v> we'll learn how to read content from a file. Because of its origins in the browser, JavaScript can't do this natively, but as you might suspect there's an NPM module to do it. We'll start by using Curl to download a file that has a list of phrases. <v ->o ranges to save it as a file,</v> and then L follows redirects automatically. It's at the learn enough CDN. It's actually just Amazon S3, which we'll see in the next section. Let's take a look at it. All right, we've got a bunch of phrases. And as you might guess, some of them are palindromes. Here's one that we've seen before. And this one. All right, our task here is to open this file, go through it line by line and output any palindromes that are detected. Now I'd like to emphasize that when I started writing this tutorial, I didn't know how to do this. And I've included in the text some of the Google searches that I used to figure out how to do things. By the way, one of the good tricks is to use Node instead of JavaScript in a search, something like "Node open file." You can also include the word JavaScript but including Node is important because that helps you find Node packages. We'll be using fs within this project, so let's install it locally. I found the file system documentation to be little confusing, so I tracked down a blog post, that's linked in the text, that helped clarify things. Let's see how FS works in the REPL. Fs exports the fs object. And now we can read the phrases.txt file using the method, read file sync, where sync stands for synchronous and can be used when you don't have to run more than one of these programs at the same time. Here's how you use it. We're gonna have some text, fs.readFileSync. Let's look at that. Let's take a look at the length. We can look at some contents too. Let's split it on new line to get an array of lines. And let's look at the first one. And this brings us to our first problem which I included intentionally. As noted in the text, we have to give read file sync the character encoding. So it's not as simple as saying, read file sync the name, you have to say, what is encoded as, and in this case, we're using UTF-8. Oops, and we already did let, so we can redefine it here. Aha! That looks better. The length is now a little different than it was before, and we split on new line, take the first element or zeroth element. Aha! A butt tuba, which is in fact a palindrome if you look at it closely. Let's write a program to verify that. We're not gonna be doing anything new other than the read file sync, so this is a good chance to practice reading code. Let's make a file called Palindrome File. We'll make a palindrome URL in the next section. Now recall that when writing a shell script, we need to have a shebang line telling our system where the executable is that we're going to use to execute this program. In this case it's Node. So in my system, that's user/local/bin/node. This is a nice trick, I'm gonna pipe it to pbcopy. This won't work if you're not on a Mac. This way I can paste it, shebang like that. I'd like to start off like this. I have to change its mode to executable. And then dot slash. All right, it's working. And now, let's incorporate the things we did before. Actually I'm gonna close this down, you can see there's no syntax highlighting, but if I close it and reopen it, it'll be highlighted properly. The reason that happens is that when I reopen it, ADN can see from the shebang line that this should be highlighted as JavaScript. It knows that Node indicates a JavaScript file. So let's get rid of that. All right, we wanna require fs, file system. And we'll also want our phrase object. I'll use mine here, my palindrome detector. You should use yours if you have it. And then I'm gonna do the same thing I did in the REPL, let text read file sync phrases.txt and then with the character encoding. And what we wanna do is go through each line and output that we detected a palindrome, if it's a palindrome. We just had a reminder in the REPL how to get an array of the phrases, we can just split on new line. Now this is an array of lines, and we can say for each line, do something with that line. And in this case, we can make a new phrase, based on the line, and then if that phrases is a palindrome, will output something to the log, which in this case is the terminal. If phrase.palindrome palindrome detected and then we can just give it a second argument here. Well actually I don't need the space because console.log outputs spaces between these strings automatically. And this is it. We're going through the file line by line, anytime we detect a palindrome, we output palindrome detected, and see if it works. Got it. There are a lot of palindromes here. Including some fun ones, Dennis sinned, Dennis and Edna sinned, we've got Dennis, Nell, Edna, Leon, Nedra, Anita, et cetera et cetera, all these people, and Ellen sinned. We also see the long variation on a man, a plan, a canal, Panama here. My favorite, and as promised, a butt tuba is also a palindrome. In the next section, we'll learn how to write this same program by accessing URL directly without having to download the file first.