Node.js fs.open() Method - GeeksforGeeks

21 Jan, 2022
Introduction: To create 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 module to access 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 accept 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 other location.
flag: The operation in which file has to be opened.
mode: Sets the mode of 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.
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:  
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 modulevar fs = require('fs'); // Open file demo.txt in read modefs.open('demo.txt', 'r', function (err, f) {  console.log('Saved!');});
Output:  
Saved! 
Explanation: The file is opened and the flag is set to read mode. After opening of file function is called to read the contents of file and store in memory. As there are no errors ‘saved’ is printed.Example 2:  
javascript
// Node.js program to demonstrate the   // fs.open() Method  // Include the fs modulevar 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!
Explanation: The file ‘demo.txt’ is opened in read and write mode, then the function is called. Output shows a number in file when we print value of ‘f’. Reference: https://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback 
Akanksha_Rai
dshaikh900
Node.js-fs-module
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
How to use an ES6 import in Node.js?
Mongoose | findByIdAndUpdate() Function
Express.js res.render() Function
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript