Node.js | fs.open() Method

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: Contents of the file. It is called after open operation is executed.

All the types of flags are described below:

Flag Description
r To open file to read and throws exception if file doesn’t exists.
r+ Open file to read and write. Throws exception if file doesn’t exists.
rs+ Open file in synchronous mode to read and write.
w Open file for writing. File is created if it doesn’t exists.
wx It is same as ‘w’ but fails if path exists.
w+ Open file to read and write. File is created if it doesn’t exists.
wx+ It is same as ‘w+’ but fails if path exists.
a Open file to append. File is created if it doesn’t exists.
ax It is same as ‘a’ but fails if path exists.
a+ Open file for reading and appending. File is created if it doesn’t exists.
ax+ It is same as ‘a+’ but fails if path exists.

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



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the    
// fs.open() Method
   
// Include the fs module
var 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!!");     
});

chevron_right


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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai

Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.