Reading a file in Node.js using a synchronous method

*Note this tutorial will be done on Ubuntu 18.04

File System(fs) is a core Node.js module that provides both synchronous and asynchronous file I/O operations. In short, the fs module provides a user an efficient and friendly way to handle reading files writing files, and much more. There are are a multitude of fs module operations available which can be viewed here https://nodejs.org/api/fs.html#fs_file_system.

Here is a short read on the differences between synchronous and asynchronous in relation to computer I/O operations : http://www.differencebetween.net/language/difference-between-synchronous-and-asynchronous/

In short, synchronous means you complete one task at a time, waiting for that task to come to an end before continuing on. Asynchronous means while a task is executing, you can move on to another task before the previous executed task finishes executing.

1. In your current working directory, create two files

→ file_system_demo.js

→ test_file.txt

2. In your “test_file.txt” add 5-10 lines of text

3. Open “file_system_demo.js”. Here is where we will be adding all code necessary to read and write to our test file.

4. Create a variable and store the File System module in it:

→ var fs = require(‘fs’);

5. To read our file using a synchronous method, add the following line of code:

→ fs.readFileSync();

This method will not access and read our file unless we pass it specific parameters. We have two parameter options, a path, and some options. The path is mandatory while the options parameter is optional.

fs.readFileSync(path,[,options]) // *Note square brackets mean the parameter is not mandatory

A “path” argument can be a <string>, <Buffer>, <URL>, or an <integer>, which must be a filename or file descriptor.

The “options” argument is where we will specify our character encoding. Since we are reading a file, we will be dealing with binary data. In order to convert the 0’s and 1’s into something readable, we pass in a character encoding argument. For the sake of this tutorial we will be using utf8.

6. Edit your previous line of code to look like this:

→ var my_file = fs.readFileSync(‘test_file.txt’, ‘utf8’);

In the above step we stored our read file into a variable so that we can access it’s data and print it to the screen.

7. Add the following line to your code to print the file information:

→ console.log(my_file);

8. Save your file and return to the root directory your two files are stored in.

9. Run the command:

→ node file_system_.js

10. The contents of your file should now be printed on your terminal!

With this synchronous method call, any lines of code placed after the “fs.readFileSync” line of execution will not be fired until “fs.readFileSync” finishes executing.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s