Home  >  Article  >  Web Front-end  >  Instructions for using the fs.open method in node.js_node.js

Instructions for using the fs.open method in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 16:26:131399browse

Method description:

Open the file asynchronously.

In POSIX systems, path is considered to exist by default (even if the file under the path does not exist)

flag flag may or may not run under a network file system.

Grammar:

Copy code The code is as follows:

fs.open(path, flags, [mode], [callback(err,fd)])

Since this method belongs to the fs module, the fs module needs to be introduced before use (var fs= require(“fs”) )

Receive parameters:

path File path

flags can be the following values

Copy code The code is as follows:

'r' - Open the file in read mode.
'r ' - Open the file in read-write mode.
'rs' - Open and read the file using synchronous mode. Instructs the operating system to ignore the local file system cache.
'rs ' - Open, read and write the file synchronously.
Note: This is not a blocking operation that puts fs.open into synchronous mode. If you want synchronous mode use fs.openSync().

'w' - Open the file in read mode or create it if it does not exist
'wx' - same as ' w ' mode, returns failure if the file exists
'w ' - Open the file in read-write mode, create the file if it does not exist
'wx ' - same as ' w ' mode, returns failure if the file exists

'a' - Open the file in append mode, creating it if it does not exist
'ax' - same as ' a ' mode, returns failure if the file exists
'a ' - Open file in read-append mode, create if file does not exist
'ax ' - same as ' a ' mode, returns failure if the file exists
mode is used to set permissions for files when creating files. The default is 0666

callback The callback function will pass a file descriptor fd and an exception err

Example:

Copy code The code 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:

Copy code The code 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);
};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn