SoFunction
Updated on 2025-03-10

NodeJs' fs read, write and delete mobile listening

NodeJs version: 4.4.4

fs

The file system module is a collection of standard POSIX file I/O operations. The methods in the file system (fs module) module are available in asynchronous and synchronous versions.

Copy and paste pictures

Create a readable stream with a write stream. Pipe through the pipeline.

var fileReadStream = (sourcePath);
var fileWriteStream = (targetPath);
(fileWriteStream);
// Listen to the closing event and know that the execution is completed('close', function() {
 ('Move successfully!');   
})

Read file()

Definition: (filename[, options], callback)

parameter:

  • filename:{String} Filename/file path
  • options:{Object} Optional parameters

encoding:{String | Null} Default = null encoding method

flag:{String} Default = 'r' The behavior of opening the file (writable, readable, etc.)

  • callback:{Function}
var fs = require('fs');
//Read the file('../lianxi/child_process.js',{
 encoding:'utf-8',
 flag:'r'
}, function(err,data){
 if(err) throw err;
 (data);
});

If the encoding method is not set for the read file here, the read file will be returned in the form of a buffer.

<Buffer 76 61 72 20 63 68 69 6c 64 5f 70 72 6f 63 65 73 73 20 3d 20 72 65 71 75 69 72 65 28 27 63 68 69 6c 64 5f 70 72 6f 63 65 73 73 27 29 3b 0d 0a 76 61 72 ... >

After setting to utf-8, the returned string is the form. as follows:

var child_process = require('child_process');...

Write a file()

Definition: (filename, data[, options], callback)

parameter:

  • 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 to file('../lianxi/child_process.js','[zqz]The data string or buffer to be written',{
 encoding:'utf8',
 mode:438,
 flag:'w'
},function(err){
})

Notice:Write files asynchronously, and replace them if the file already exists.

Open file()

Definition: (path, flags[, mode], callback)

parameter:

  • path: file/file path
  • flags: the behavior of opening a file
  • mode: Set file mode (permissions), the default permission for file creation is 0666 (readable and writable).
  • callback: callback function
//Open the file('../lianxi/child_process.js','r+',0666,function(err,data){
})

Add data to the file()

Definition: (filename, data[, options], callback)

parameter:

  • 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('../lianxi/child_process.js', 'Async added string or buffer', {
 encoding:'utf8',
 mode:438,
 flag:'a'
}, function(err){
});

Notice:Add data to the file asynchronously, and if the file does not exist, a file will be created.

Delete file()

Definition:(path, callback)

var fs = require('fs');
('./t/',function (err) {
 if(err) throw err;
 ('success')
})


Create a file()

Definition:(path, flags[, mode], callback)

You can also use create files.

("", "w",function (err) {
});


Delete folder()

Definition:(path, callback)

('./t/a',function (err) {
 if(err) throw err;
 ('success')
})

Create folder()

Definition:(path[, mode], callback)

Parameter: mode default is to 0777.

('./t/a',0777,function (err) {
 if(err) throw err;
 ('success')
})

File listening ( )

Definition: (filename[, options][, listener])
Definition: (filename[, options], listener)

('', function (event, filename) {
});
('', function(curr, prev){
});

flags

Flag describe
r Open the file in read mode. Throw an exception if the file does not exist.
r+ Open the file in read and write mode. Throw an exception if the file does not exist.
rs Read files in a synchronous manner.
rs+ Read and write files in a synchronous manner.
w Open the file in write mode and create it if the file does not exist.
wx Similar to 'w', but if the file path exists, the file write fails.
w+ Open the file in read-write mode, and create it if the file does not exist.
wx+ Similar to 'w+', but if the file path exists, the file read and write failure.
a Open the file in append mode and create it if the file does not exist.
ax Similar to 'a', but if the file path exists, file appending fails.
a+ Open the file in read append mode and create it if the file does not exist.
ax+ Similar to 'a+', but if the file path exists, the file read append failed.

The above is the NodeJs FS read, write and delete mobile monitoring introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!