How To Get, Open, Read and Rename File Using Node fs
source link: https://www.js-tutorials.com/nodejs-tutorial/how-to-get-openread-and-rename-file-using-node-fs/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How To Get, Open, Read and Rename File Using Node fs
This Nodejs tutorial help to understand accessing of the file system using nodejs module. The fs()
file system module allows you to work with a file system on your system.
As we know, All node.js file system operations have synchronous and asynchronous forms. The fs module enables interacting with the file system in a way modeled on standard POSIX functions.
We’ll cover following opeartion on a file using Node js File System Module:
- Get a File Stats
- Open a File
- Read a File
- Rename a File
How To Get, Open, Read and Rename File
We’ll learn here to open a file, read a file, read a file stat and rename a file using the Node file system module.
File Modes available
There are following file modes available into fs Nodejs module:
Flag Description
r Open a file for reading. An exception throws if a file does not exist.
r+ Open a file for reading and writing. An exception occurs if the file does not exist.
rs Open a file for reading in synchronous mode.
rs+ Open a file for reading and writing.
w Open a file for writing. The file is created , if it does not exist otherwise truncated.
w+ Open a file for reading and writing. If the file does not exist, it is created or truncated(if it exists).
a Open a file for appending. If the file does not exist, it is created.
a+ Open file for reading and appending. If the file does not exist, it is created.
How To Get File Information Using fs
Following is the syntax of the method to get the information about a particular file.
fs.stat(path, callback)
Take a look at a simple Node file stat example.
The stats
variable has all file information.
Open a File using Node fs module
The syntax is following to open a file using open()
method, this method is a part of Node js File System Module.
The Syntax:
fs.open(path, flags[, mode], callback)
Let’s use the open()
method of the Node fs module to open a file.
If the file exists then, it will open the file in the mode of r+
.
Read a File in Nodejs
Reading a file content is a very common operation in the file system, We’ll use readFile()
method to read file content. We’ll take an example and understand how we can read the file using the nodejs fs
module. We’ll read the file and display the content of the file on the webpage.
You can access the webpage at this URL: https://localhost:3010
How To Rename a File Using NodeJS Module
We can rename the file using nodejs file system as well. We’ll use rename()
method to rename a file. The fs.rename()
method renames the specified file.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK