The Wayback Machine - https://web.archive.org/web/20230530161830/https://www.geeksforgeeks.org/node-js-fs-open-method/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Node.js fs.open() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

To create the file, to write to a file, or to read a file fs.open() method is used. fs.readFile() is only for reading the file and similarly fs.writeFile() is only for writing to a file, whereas fs.open() method does several operations on a file. First, we need to load the fs class which is a module to access the physical file system. For it require method is used. For example: var fs = require(‘fs’); 

Syntax: 

fs.open( filename, flags, mode, callback )

Parameter: This method accepts four parameters as mentioned above and described below:  

  • filename: It holds the name of the file to read or the entire path if stored at another location.
  • flag: The operation in which the file has to be opened.
  • mode: Sets the mode of the file i.e. r-read, w-write, r+ -readwrite. It sets to default as readwrite.
  • callback: It is a callback function that is called after reading a file. It takes two parameters: 
    • err: If any error occurs.
    • data: A file descriptor, used by subsequent file operations. A file descriptor is a handle used to access a file. It is a non-negative integer uniquely referencing a specific file.

All the types of flags are described below:

FlagDescription
rTo open the file to read and throws an exception if the file doesn’t exist.
r+Open the file to read and write. Throws an exception if the file doesn’t exist.
rs+Open files in synchronous mode to read and write.
wOpen file for writing. A file is created if it doesn’t exist.
wxIt is the same as ‘w’ but fails if the path exists.
w+Open the file to read and write. A file is created if it doesn’t exist.
wx+It is the same as ‘w+’ but fails if the path exists.
aOpen the file to append. A file is created if it doesn’t exist.
axIt is the same as a but fails if the path exists.
a+Open the file for reading and appending. A file is created if it doesn’t exist.
ax+It is the same as ‘a+’ but fails if the path exists.

Below examples illustrate the fs.open() method in Node.js:

Example 1: 

javascript




// Node.js program to demonstrate the
// fs.open() Method
 
// Include the fs module
const fs = require('fs');
 
// Open file demo.txt in read mode
fs.open('demo.txt', 'r', function (err, f) {
    console.log('Saved!');
});

Output:  

Saved! 

The file is opened and the flag is set to read mode. After the opening of the file function is called to read the contents of the file and store them in memory. As there are no errors ‘saved’ is printed.

Example 2: 

javascript




// Include the fs module
// Node.js program to demonstrate the
// fs.open() Method
 
// Include the fs module
const fs = require('fs');
 
console.log("Open file!");
 
// To open file in write and read mode,
// create file if doesn't exists.
fs.open('demo.txt', 'w+', function (err, f) {
    if (err) {
        return console.error(err);
    }
    console.log(f);
    console.log("File opened!!");
});

Output: 

Open file!
10
File Opened!

The file ‘demo.txt’ is opened in read and write mode, then the function is called. The output shows a number in the file when we print the value of ‘f’. 

Reference: https://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback


My Personal Notes arrow_drop_up
Last Updated : 30 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials