FS Module
FS Module
Node.js is a platform that uses Chrome's V8 JavaScript engine, allowing developers to use JavaScript to
create server-side applications that generate dynamic content for web clients. What sets Node.js apart
are its event-driven and non-blocking I/O features. Node.js includes a built-in module called FS (File
System) that allows users to manage files by creating, reading, deleting, and performing other file
operations. To use this module, developers can call the "require()" method, which provides access to POSIX
functions wrapped by Node.js to enable both synchronous and asynchronous file system operations,
depending on the user's requirements.
Example : Create a text file named input.txt with the following content:
PW Skills
Asynchronous read :
Synchronous Read :
Output :
Synchronous read: PW Skills
Opening a file:
To perform operations like creating, reading, or writing a file in Node.js, the "fs" module provides several
methods, including the "fs.open()" method. Unlike the "fs.readFile()" and "fs.writeFile()" methods, which are
limited to reading or writing files, the "fs.open()" method can perform multiple operations on a file. To use
this method, you first need to load the "fs" module, which provides access to the physical file system.
Parameters:
path: It holds the name of the file to read or the entire path if stored at other locations.
flags: Flags indicate the behavior of the file to be opened. All possible values are ( r, r+, rs, rs+, w, wx, w+, wx+,
a, ax, a+, ax+).
mode: Sets the mode of file i.e. r-read, w-write, r+ -readwrite. It sets to default as readwrite.
data: Contents of the file. It is called after the open operation is executed.
Example: Create a js file named main.js having the following code to open a file input.txt for reading and
writing.
Reading a File:
The fs.read() method is used to read the file specified by fd. This method reads the entire file into the buffer.
Parameters:
buffer: This is the buffer that the data will be written to.
position: This is an integer specifying where to begin reading from in the file. If the position is null, data will be
read from the current file position.
callback: It is a callback function that is called after reading of the file. It takes two parameters:
The "fs.writeFile()" method is used to write data to a file asynchronously in Node.js. If the file already exists,
this method will overwrite it by default. However, the behavior of this method can be modified using the
"options" parameter.
Parameters:
path: It is a string, Buffer, URL, or file description integer that denotes the path of the file where it has to be
written. Using a file descriptor will make it behave similarly to fs.write() method.
data: It is a string, Buffer, TypedArray, or DataView that will be written to the file.
options: It is a string or object that can be used to specify optional parameters that will affect the output. It
has three optional parameters:
encoding: It is a string value that specifies the encoding of the file. The default value is ‘utf8’.
mode: It is an integer value that specifies the file mode. The default value is 0o666.
flag: It is a string value that specifies the flag used while writing to the file. The default value is ‘w’.
callback: It is the function that would be called when the method is executed.
Appending to a File
The fs.appendFile() method is used to synchronously append the data to the file.
or
Parameters explanation:
data: Mandatory and contains the data that you append to the file.
Callback: Mandatory function and is called when appending data to file is completed.
Output:
Output:
Data is appended to file successfully.
PW Skills
PW Skills
Learn Node.js
The function fs.close() is utilized to close a specific file descriptor in an asynchronous manner, which
effectively erases the file linked with it. Once the file is closed, the file descriptor can be used for other files.
However, if fs.close() is executed on a file descriptor while another operation is still in progress, the outcome
may be uncertain.
fs.close(fd, callback)
Parameters:
fd: It is an integer that denotes the file descriptor of the file for which to be closed.
Delete a File
The fs.unlink() method is used to remove a file or symbolic link from the filesystem. This function does not work
on directories, therefore it is recommended to use fs.rmdir() to remove a directory.
fs.unlink(path, callback)
Parameters:
path: It is a string, Buffer or URL which represents the file or symbolic link which has to be removed.
Output:
deleting...