How we can open a file in Node JS?
open() function is used to opens a file by passing a file name.
Syntax : fs.open(path, flags[mode], callback)
Parameters
- path : It is a string having file name with complete path
- flags : It indicates the behavior of the file to be opened
- mode : It sets the file mode like permission
- callback : gets two arguments (err, fd)
BY Best Interview Question ON 12 Jun 2020
Example
var fs = require("fs");
// Asynchronous - Open a File
console.log("open file");
fs.open('file.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}
console.log("File opened");
});