Hey reader👋Hope you are doing well😊
In the last post we have discussed about File System Module in NodeJS. In this post we are going to discuss more about File System Module in NodeJS.
So let's get started🔥
(In the last post we have seen how to perform read and write operations both synchronously and asynchronously. In this post we are going to see some other operations.)
Append data in file operation
Appending data means adding more data at the end of file. We have our "read.txt" file from last post in which we are going to append few more details and then we will read the existing file.
Asynchronous Approach
For appending asynchronously use appendFile()
method.
The appendFile()
method takes file in which data is to be appended then it takes data itself and then it takes a callback function. To specify encoding you can use options
which can be an object or string.
So you can see we have easily added more data to the file.
Synchronous Approach
To perform appending synchronously use appendFileSync()
.
Create a folder
You can create a folder using FileSystem Module.
Asynchronous Approach
To create folder asynchronously use mkdir()
method.
You can see that here we have used one module that is "Path" module, we are using it so that we can easily use and manipulate paths of folder and file. We have created a folder named as "folder1" using mkdir()
method in which we have given name of folder (we can give path too) and then we have given callback function. Then we have created a file with the file name "bio.txt" and added that file to the previosly created folder using join()
method that we have imported from "Path" module. And at last we have read the contents of file. To recursively create multiple files at once we can use options
parameter in which set recursive
to "true".
Note-: Use async/await to prevent any necessary errors while using asynchronous operations.
Synchronous Approach
To create folder synchronously use mkdirsync()
method.
Rename File
Asynchronous Approach
To rename folder/file asynchronously use rename()
method.
(I have added this snippet of code to above code.)
In the rename()
method I have given old path of file then new path (with new name) and a callback function.
Here you can see our order of functions performed are different as we are doing all the operations asynchronously.
Synchronous Approach
To rename folder/file asynchronously use renameSync()
method.
Delete File
Asynchronous Approach
To rename folder/file asynchronously use rmdir()
and unlink()
methods respectively.
We got the path of folder in folderPath
variable using join()
method in which process.cwd()
returns the path of current working directory in which we appended our folder name. Then we have used unlink()
method to remove file from folder. In this method we have given path of file to be removed and a callback function. Then we have deleted folder using rmdir()
method.
Note -: Use async/await to get rid of any errors.
Synchronous Approach
To rename folder/file synchronously use rmdirSync()
and unlinkSync()
methods respectively.
Close File
The fs.close()
method is used to asynchronously close the given file descriptor thereby clearing the file that is associated with it. This will allow the file descriptor to be reused for other files. Calling fs.close()
on a file descriptor while some other operation is being performed on it may lead to undefined behavior.
fd : It is an integer that denotes the file descriptor of the file for which to be closed.
-
callback: It is a function that would be called when the method is executed.
- err: It is an error that would be thrown if the method fails.
So all the operations defined in this and previous blogs are CRUD (Create, Read, Update and Delete) operations. These are extremely important operations.
So this is it for this blog I hope you have understood all the operations very well. In the upcoming blog we are going to read about other modules. Till then stay connected and don't forget to follow me.😊
Thankyou 💜
Top comments (0)