Method description:
Open the file asynchronously.
In POSIX systems, path is considered to exist by default (even if the file under this path does not exist)
The flag identifier may or may not be running under the network file system.
grammar:
The code copy is as follows:
fs.open(path, flags, [mode], [callback(err,fd)])
Since this method belongs to the FS module, it is necessary to introduce the FS module before use (var fs= require("fs") )
Receive parameters:
path file path
flags can be the following values
The code copy is as follows:
'r' - Open the file in read mode.
'r+' - Open the file in read and write mode.
'rs' - Open and read files using synchronization mode. Instructs the operating system to ignore the local file system cache.
'rs+' - Open in a synchronous manner, read and write to the file.
Note: This is not a blocking operation that makes fs.open become synchronous mode. If you want synchronous mode, use fs.openSync().
'w' - Open the file in read mode, create if the file does not exist
'wx' - Like 'w' mode, it returns a failure if the file exists
'w+' - Open the file in read and write mode, create if the file does not exist
'wx+' - same as 'w+' mode, it returns a failure if the file exists
'a' - Open the file in append mode, create if the file does not exist
'ax' - Like ' a ' mode, it returns a failure if the file exists
'a+' - Open the file in read append mode, create if the file does not exist
'ax+' - same as 'a+' mode, it returns a failure if the file exists
mode is used to set permissions for files when creating files, default is 0666
The callback callback function will pass a file descriptor fd and an exception err
example:
The code copy is as follows:
var fs = require('fs');
fs.open('/path/demo1.txt', 'a', function (err, fd) {
if (err) {
throw err;
}
fs.futimes(fd, 1388648322, 1388648322, function (err) {
if (err) {
throw err;
}
console.log('futimes complete');
fs.close(fd, function () {
console.log('Done');
});
});
});
Source code:
The code copy is as follows:
fs.open = function(path, flags, mode, callback) {
callback = makeCallback(arguments[arguments.length - 1]);
mode = modeNum(mode, 438 /*=0666*/);
if (!nullCheck(path, callback)) return;
binding.open(pathModule._makeLong(path),
stringToFlags(flags),
mode,
callback);
};