Synchronous Reading
Let's take a quick look at reading a file synchronously and asynchronously. The first thing you need to do is require the fs (File System) package.
Listing 1
const fs = require("fs");
To read a file synchronously, the fs package provides a readFileSync() method, which takes one or two arguments. The first is the filename and the second optional arument is an options object, which when supplied, specifies the encoding to use and the read mode.
Listing 2
var data = fs.readFileSync("file.txt");
console.log(data);
Note that the readFileSync() method returns data, which is either of type string or Buffer. When no encoding option is supplied, a Buffer object is returned. We can convert the Buffer object to an encoded string using the toString() method as shown below.
Listing 3
var data = fs.readFileSync("test.txt");
console.log(data.toString("UTF8"));
The code below shows how to pass the encoding by the optional options object. The output is the same as the code above.
Listing 4
var data = fs.readFileSync("test.txt", { encoding : "UTF8" });
console.log(data);
Asynchronous Reading
Reading files synchronously will block Node.js event loop, which means if you are reading large files, the execution of your code will hault until the file has been read. Node.js provides an asynchronous method to read files, which is used when blocking Node.js event loop is not an option. The code sample below shows how to read a file asynchronously.
Listing 5
fs.readFile("file.txt", { encoding : "UTF8" }, (err, data) => {
console.log(data);
});
Notice that the readFile() method does not return any data but rather accepts a callback function with two arguments (err and data). The callback function is executed when the file has been read or an error occured. You should check the err variable before you attempt to process the data variable.
Node.js also provides synchronous and asynchronous methods to write data to a file. The following code shows how to write data to a file synchronously.
Listing 6
fs.writeFileSync("myfile.txt", "Hello World", { encoding : "UTF8" });
To write a file asynchronously use the writeFile() method.
Listing 7
fs.writeFile("myfile.txt", "Hello World", { encoding : "UTF8" }, (err) => {
// handle error here
});
-
A JavaScript Implementation Of The Logo Programming Language - Part 2
Part 2 of A Javascript Implementation Of The Logo Programming Language. In the previous article I explained how to develop a simple lexer. In this part we develop a TokenCollection class to help traverse the array of tokens returned from the lexer.
10 June 2020 - 2522 views -
A JavaScript Implementation Of The Logo Programming Language - Part 1
In this four part article, I explain how to develop the iconic Logo programming language in JavaScript. In this part, we discuss how to take a source input and convert it into a series of tokens.
06 June 2020 - 4712 views -
Generating Web API Keys
If you're building a REST API, chances are you're going to need to generate secure random API keys. In this article, I explain how to use the Node.js crypto module to generate random secure API keys.
29 May 2020 - 7290 views -
Port Scanner
A simple port scanner to scan a range of ports.
31 January 2020 - 3194 views -
Sorting Algorithms
An introduction to sorting algorithms.
29 November 2019 - 2155 views -
Simple Http Server
This article explains how to create a simple HTTP server using Node.js without using any dependencies.
04 September 2019 - 2047 views -
Reading From Console
Node.Js provides several ways to read user input from the terminal. This article explains how to use the process object to read user input.
16 July 2019 - 2594 views