Parse the fs read/write of NodeJs and delete the mobile listener

Source: Internet
Author: User
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:

  • Filename: {String} file name/file path

  • Options: {Object} (optional)

Encoding: {String | Null} default = null encoding method

Flag: {String} default = 'R' file opening behavior (writable, readable, etc)

  • Callback: {Function}

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'

  • Callback {Function}

// 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'

  • Callback {Function}

// 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.