. Advertisement .
..3..
. Advertisement .
..4..
This article is aimed to wind your way out of the headache of reading files using fs.readFileSync() in NodeJS. Wait for no more but scroll down for further helpful information to save in the bank!
What Is The Fs.readFileSync() Method?
The fs.readFileSync() is an application programming interface included into the fs module. It is of great use in reading files and returning their contents.
As you may know, with the fs.readFile() function, we can read files in a non-blocking asynchronous manner. Then what about the fs.readFileSync() method?
With such a feature, we can read files synchronously, instructing node.js to halt all concurrent processes while accessing the current file!
To put it another way, when the fs.readFileSync() method is invoked, the original node program pauses while it waits for the fs.readFileSync() function to complete. Once it does, the remaining node program is then run.
Syntax Of The Fs.readFileSync() Method
fs.readFileSync( path, options )
In which, path is mandatory, that is the used text file’s relative path to the document you wish to read (string type).
A URL type route is also possible as a path in this feature. Additionally, the file may be a file descriptor. Just put the filename in quotes if both files are in the same folder.
What about the options?
It is an optional parameter that contains a flag, an encoding, and data definition in the encoding. The flag holds information about file operations, and its default value of null returns a raw buffer. ‘r’ is the default value for it.
The operation flag and the encoding can alternatively be sent using an object for the options parameter as shown below:
fs.readFileSync("./data.txt", "utf8");
// or
fs.readFileSync("./data.txt", { encoding: "utf8", flag: "r" });
Accessing Files Adopting The Fs.readFileSync() Method
Let’s say you have a file called data.txt that has the following information:
Good morning
Many greats from data.txt
It’s a pleasure to meet you
See ya
Belows is how you employ the fs.readFileSync() function to read the file.
Running the code:
const fs = require("fs");
const path = "./data.txt";
try {
const data = fs.readFileSync(path, "utf8");
console.log("File content:", data);
} catch (err) {
console.error(err);
}
Output:
File content: Good morning
Many greats from data.txt
It’s a pleasure to meet you
See ya
The techniques will also let you retrieve the raw buffer if “utf8” encoding is in operation. Here is an illustration of the output for you to grasp it more:
File content: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64
0a 47 72 65 65 74 69 6e 67 73 20 66 72 6f 6d 20 64 61 74
61 2e 74 78 74 0a 48 6f 70 65 20 79 6f 75 20 68 61 76 65
20 ... 35 more bytes>
Also, bear in mind to have the encoding option if you want the output under a readable-for-human format.
The Bottom Line
This instruction on how to read files by adopting the fs.readFileSync() hopefully will bring you valuable insights. Now, you’ve got the package on what you yearn to know, let’s get down the road and experiment the programming trial your own way! And don’t forget to check out our other intriguing articles on multiple other methods to read files (such as reading files into an array in php). See you then!
Leave a comment