This article mainly introduces the fs read/write deletion mobile listening of NodeJs, which is very good and has reference value. If you need it, you can refer to the next article to introduce the fs read/write deletion mobile listening of NodeJs, it is very good and has reference value. If you need it, you can refer to it.
NodeJs version: 4.4.4
Fs
The file system module is a collection of standard POSIX file I/O operations. The methods in the Node. js File System (fs module) module are both asynchronous and synchronous.
Copy and paste Images
Create a readable stream and a write stream. Pipeline pipe.
Var fileReadStream = fs. createReadStream (sourcePath); var fileWriteStream = fs. createWriteStream (targetPath); fileReadStream. pipe (fileWriteStream); // The Listener closes the event and the execution is completed. on ('close', function () {console. log ('moved successfully! ');})
Read a file (fs. readFile)
Definition: fs. readFile (filename [, options], callback)
Parameters:
Encoding: {String | Null} default = null encoding method
Flag: {String} default = 'R' file opening behavior (writable, readable, etc)
Var fs = require ('fs'); // read the file fs. readFile ('.. /lianxi/child_process.js ', {encoding: 'utf-8', flag: 'R'}, function (err, data) {if (err) throw err; console. log (data );});
If the encoding method is not set, the Read File is returned as a buffer.
If it is set to UTF-8, the returned result is in the string format. As follows:
var child_process = require('child_process');...
Write a file (fs. writeFile)
Definition: fs. writeFile (filename, data [, options], callback)
Parameters:
Filename: {String}
Data: {String | Buffer}
Options: {Object}
Encoding: {String | Null} default = 'utf8'
Mode: {Number} default = 438 (aka 0666 in Octal)
Flag: {String} default = 'W'
// Write File fs. writeFile ('.. /lianxi/child_process.js ',' [zqz] data string or buffer ', {encoding: 'utf8', mode: 438, flag: 'W '}, function (err ){})
Note:Asynchronous file writing. If the file already exists, replace it.
Open a file (fs. open)
Definition: fs. open (path, flags [, mode], callback)
Parameters:
Path: File/file path
Flags: file opening Behavior
Mode: sets the file mode (permission). The default permission for file creation is 0666 (readable and writable ).
Callback: callback function
// Open the file fs. open ('../lianxi/child_process.js', 'r + ', 0666, function (err, data ){})
Add data to a file (fs. appendFile)
Definition: fs. appendFile (filename, data [, options], callback)
Parameters:
Filename: {String}
Data: {String | Buffer}
Options: {Object}
Encoding {String | Null} default = 'utf8'
Mode {Number} default = 438 (aka 0666 in Octal)
Flag {String} default = 'A'
// Add data to the file fs. appendFile ('.. /lianxi/child_process.js ', 'asynchronously added string or buffer', {encoding: 'utf8', mode: 438, flag: 'A'}, function (err) {});
Note:Asynchronously add data to a file. If the file does not exist, a file is created.
Delete a file (fs. unlink)
Definition: fs. unlink (path, callback)
Var fs = require ('fs'); fs. unlink ('. /t/index.html ', function (err) {if (err) throw err; console. log ('success ')})
Create a file (fs. open)
Definition: fs. open (path, flags [, mode], callback)
You can also use fs. open to create a file.
fs.open("test.txt", "w",function (err) {});
Delete a folder (fs. rmdir)
Definition: fs. rmdir (path, callback)
Fs. rmdir ('./t/A', function (err) {if (err) throw err; console. log ('success ')})
Create a folder (fs. mkdir)
Definition: fs. mkdir (path [, mode], callback)
Parameter: the default mode is to 0777.
Fs. mkdir ('./t/A', 0777, function (err) {if (err) throw err; console. log ('success ')})
File listening (fs. watch fs. watchFile)
Definition: fs. watch (filename [, options] [, listener])
Definition: fs. watchFile (filename [, options], listener)
fs.watch('test.js', function (event, filename) {});fs.watchFile('test.js', function(curr, prev){});
Flags
Flag |
Description |
R |
Open the file in Read mode. If the file does not exist, an exception is thrown. |
R + |
Open a file in read/write mode. If the file does not exist, an exception is thrown. |
Rs |
Read files in synchronous mode. |
Rs + |
Read and Write files in synchronous mode. |
W |
Open the file in write mode. If the file does not exist, it is created. |
Wx |
Similar to 'w', but if the file path exists, the file fails to be written. |
W + |
Open the file in read/write mode. If the file does not exist, it is created. |
Wx + |
Similar to 'W + ', but if the file path exists, the file read/write fails. |
A |
Open the file in append mode. If the file does not exist, it is created. |
Ax |
Similar to 'A', but if the file path exists, the file append fails. |
A + |
Open the file in read append mode. If the file does not exist, it is created. |
Ax + |
Similar to 'a + ', but if the file path exists, the file read and append fails. |
The above describes how to parse the fs read/write of NodeJs to delete mobile listeners. For more information, see other related articles in the first PHP community!