Nodejs is an open-source javascript runtime to run javascript outside of the browser. It is a popular choice for backend development. Like every other programming language Nodejs also provides us with the built-in libraries to interact with the system or machine. In Nodejs we call these libraries modules.
Modules basically are divided into 3 categories:
- Node core modules
- Local modules
- 3rd party modules (like npm modules).
File System module: File System is a core module by a node to interact with files and folders. It provides various events and methods to access and manipulate files and folders in our machine.
Some of the operations it provides to manipulate files are:
- Create and delete files or folders.
- Accessing and renaming files or folders.
- Read, write and append files.
- Changing permission and owner of file or folder.
To use the File System module in your program, you need to fs node core module:
Syntax:
const fs = require('fs')
Now before using the fs module you need to understand that there are two approaches to use fs module methods:-
- Using synchronous methods
- Using asynchronous methods
Now the question comes which should we choose synchronous methods or asynchronous methods. Let’s discuss what is the difference between synchronous methods and asynchronous methods of fs module.
Synchronous and Asynchronous methods:
Synchronous methods use functions that are blocking in nature. Blocking functions blocks the execution of the next instruction or piece of code till the current operation is not completed. Synchronous methods wait for the current operation to be finished and then it continues with the execution of the next instruction. But this creates problems when the current operation takes a lot of time.
Example: Suppose our server after getting a request needs to create a file and then write some text in it and then respond back to the client. If for creating and writing on a file we use a synchronous approach then for each coming request we would block the execution till we are done with operations and responding to that client. If a lot of requests come together we are basically blocking other users till we are finished with the first user. The last user would have to wait a lot to get this response and that would be bad.
Asynchronous methods are non-blocking in nature as they never wait for the current operation to complete. While calling an asynchronous method we have to pass a callback function as an argument. When an asynchronous function is called it is registered or pushed by Event loop to a queue and the next line of code is executed. In the background, our asynchronous function is executed and when it completes, the callback function we passed as an argument is pushed in the callback queue and is executed when its turn comes.
Basically, the catch here is to understand that asynchronous methods do the operation in the background and do not block the
execution of code.
Synchronous and Asynchronous code execution
File System provides us with synchronous as well as the asynchronous version of methods.
NOTE – We should always use the asynchronous version of methods in our code if we have a choice. Synchronous methods should only be used in top-level code because top-level code is executed only once or only in situations where we are certain that operation won’t take long (lightweight operation).
Operations on file:
- Reading a file – The simplest way to read a file is using fs.readFile() method. This method takes care of opening and closing the file and it loads the file contents in the memory for us to use in our program.
Syntax :
fs.readFile(path,options,callback);
Parameters :
- path – the name or path of the file we want to read from
- options – this is an optional argument and generally we pass encoding which is ‘utf-8‘
- callback – executed when we have read the file. It takes two parameters
- error – if any error occurs
- data – contents of the file
Example – Create a file name app.js and also create a text file input.txt
Project Structure –
app.js
const fs = require( "fs" );
fs.readFile( "input.txt" , "utf-8" , (error, data) => {
if (error) {
console.log( "File not found" );
} else {
console.log(data);
}
});
|
Run app.js using the command below :
node app.js
Output:
Hurrah, now you know how to read a file.
Example 2: We will use the synchronous version, which is fs.readFileSync(). Every other method discussed below also has a synchronous version. To use that just add Sync at the end of the function name and remove the callback parameter.
Javascript
const fs = require( "fs" );
try {
const data = fs.readFileSync( "input.txt" , "utf-8" );
console.log(data);
} catch (error) {
console.log(error);
}
|
Output :
Hurrah, now you know how to read a file synchronously.
- Writing in a file – We can write in our file using fs.writeFile() function . This function writes data to the file by replacing the existing contents of the file. Also if there is no such file then it creates the file and writes data on it.
Syntax:
fs.writeFile(path, data, options, callback)
Parameters:
- path – The name or path of the file to write to.
- data – Content to write to file
- options – Optional but we usually specify the encoding – ‘utf-8‘.
- callback – Executed after we have written on file.
- error – if any error occurs,
Example – Create an empty text file – output.txt and also app.js file –
Project Structure :
app.js
const fs = require( "fs" );
const str = "Learning how to write to a file." ;
fs.writeFile( "output.txt" , str, "utf-8" , (error) => {
if (error) {
console.log(error);
}
else {
console.log( "Successfully written!!" );
}
fs.readFile( "output.txt" , "utf-8" , (error, data) => {
if (error) {
console.log(error);
}
else {
console.log(data);
}
});
});
|
Run app.js file with command –
node app.js
Output: A new file with the name output.txt will be created and data will be written on it. But file already existed then all its contents will be deleted and replaced by the data we wrote.
Successfully written!!
Learning how to write to a file.
- Appending in a file – We can append data to our file using fs.appendFile() . Unlike fs.writeFile() it doesn’t replace the content of the file but rather adds the data to it. If the file is not found then it creates the file and adds the data to it.
Syntax:
fs.appendFile(path, data, options, callback)
Parameters –
- path – The name or path of the file.
- data – Content to be added in the file.
- options – We generally specify encoding – ‘utf-8‘
- callback – Executed after we have appended the file
- error – if there is an error
Example – Create a file app.js and also an empty output.txt file –
Project Structure:
output.txt: Initially contains –
Learning how to write to a file. (Previous content)
app.js
Javascript
const fs = require( "fs" );
const data = "\nLearning how to append to a file. (New content)" ;
fs.appendFile( "output.txt" , data, "utf-8" , (error) => {
if (error) {
console.log(error);
}
else {
console.log( "Successfully written!!" );
}
fs.readFile( "output.txt" , "utf-8" , (error, data) => {
if (error) {
console.log(error);
}
else {
console.log(data);
}
});
});
|
Run the app.js with the command –
node app.js
Output: The data we provide will be appended in output.txt and we will get the below output –
Successfully written!!
Learning how to write to a file. (Previous content)
Learning how to append to a file. (New content)
There are a lot of other methods of fs module that are used to perform various types of operations on files – fs.rename() , fs.unlink(), fs.open(), fs.close() etc.
Let’s create an offline judge project using the file system module:
A demo of an offline judge: Suppose you have a JSON file test-cases.json which contains all the input test cases for a problem and its output.
Project Structure –
test-cases.json
{
"problem": 121,
"input": [
[5, 3, 4, 2, 1],
[8, 100, 47, 999, 504, 771, 21, 53, 45, 660, 9],
[0, 0, 7, 1, 4, 7, 3, 5],
[1],
[]
],
"output": [3, 673, 19, 1, 10]
}
Problem Statement: Find the difference between the odd and even sum
Javascript
function diffOfOddEvenSum(arr) {
}
|
Now, to create an offline judge you need to create an app.js file and write the code –
app.js
const fs = require( "fs" );
const funct = require( "./index" );
function diffOfOddEvenSum(arr) {
let odd = 0;
let even = 0;
arr.forEach((element) => {
if (element % 2 == 1) {
odd += element;
} else even += element;
});
return odd - even;
}
const jsonData = fs.readFileSync( "test.json" , "utf-8" );
const testCases = JSON.parse(jsonData);
const inputs = testCases.input;
const expectedOutput = testCases.output;
let result = "" ;
inputs.forEach((input, index) => {
const output = diffOfOddEvenSum(input);
if (output == expectedOutput[index]) {
result += `Test Case ${index + 1}- passed \n`;
} else {
result += `Test Case ${index + 1}- failed \n`;
}
});
fs.writeFileSync( "result.txt" , result, "utf-8" );
|
Run app.js using command –
node app.js
Output – In our result.txt file, we have
Test Case 1- passed
Test Case 2- passed
Test Case 3- passed
Test Case 4- passed
Test Case 5- failed
In this way, we can simply create an offline judging tool using file system core module
Last Updated :
31 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...