This article mainly introduces the fs read, write, delete and mobile monitoring of NodeJs. It is very good and has reference value. Friends in need can refer to it
NodeJs version: 4.4.4
fs
The file system module is a collection that encapsulates standard POSIX file I/O operations. Methods in the Node.js Filesystem (fs module) module have both asynchronous and synchronous versions.
Copy and paste pictures
Create a readable stream and a write stream. via pipe.
var fileReadStream = fs.createReadStream(sourcePath); var fileWriteStream = fs.createWriteStream(targetPath); fileReadStream.pipe(fileWriteStream); //监听关闭事件得知执行完成 fileWriteStream.on('close', function() { console.log('移动成功!'); })
Read file (fs.readFile)
Definition: fs.readFile( filename[, options], callback)
Parameters:
filename:{String} file name/file path
options:{Object} Optional parameters
encoding:{String | Null} Default = null Encoding method
flag:{String} Default = 'r' File opening behavior (writable, readable, etc.)
callback:{Function}
var fs = require('fs'); //读取文件 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 when reading files here, the read files will be returned in the form of 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 value is in the form of a string. As follows:
var child_process = require('child_process');...
Write file (fs.writeFile)
Definition :fs.writeFile(filename, data[, options], callback)
Parameters:
filename:{String}
data:{String | Buffer}
options:{Object}
mode:{Number} Default = 438 (aka 0666 in Octal)
flag:{String} Default = 'w'
//写入文件 fs.writeFile('../lianxi/child_process.js','[zqz]要写入的数据字符串或者buffer',{ encoding:'utf8', mode:438, flag:'w' },function(err){ })
Note: Write the file asynchronously, replacing the file if it already exists.
Open file (fs.open)
Definition: fs.open(path, flags[, mode], callback)Parameters:
//打开文件 fs.open('../lianxi/child_process.js','r+',0666,function(err,data){ })
Add data to the file (fs.appendFile)
Definition: fs.appendFile(filename, data[, options], callback)Parameters:mode {Number} Default = 438 (aka 0666 in Octal)
flag {String} default = 'a'
//给文件添加数据 fs.appendFile('../lianxi/child_process.js', '异步添加的字符串或buffer', { encoding:'utf8', mode:438, flag:'a' }, function(err){ });
Note:Add data to the file asynchronously. If the file does not exist, a file will be created.
Delete 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('成功') })
Create file (fs.open)Definition: fs.open( path, flags[, mode], callback)
You can also use fs.open to create files.
fs.open("test.txt", "w",function (err) { });
Definition: fs.rmdir(path, callback)
fs.rmdir('./t/a',function (err) { if(err) throw err; console.log('成功') })
Definition: fs.mkdir(path[, mode], callback)
Parameter: mode The default is to 0777.fs.mkdir('./t/a',0777,function (err) { if(err) throw err; console.log('成功') })
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){ });
Flag | Description |
---|---|
Open in read mode document. Throws an exception if the file does not exist. | |
Open the file in read-write mode. Throws an exception if the file does not exist. | |
Read files synchronously. | |
Read and write files synchronously. | |
Open the file in writing mode, creating it if it does not exist. | |
Like 'w', but if the file path exists, file writing fails. | |
w+ | Open the file in read-write mode and create it if it does not exist. |
wx+ | Similar to 'w+', but if the file path exists, file reading and writing will fail. |
a | Open the file in append mode, creating it if it does not exist. |
ax | Similar to 'a', but if the file path exists, file appending fails. |
a+ | Open the file in read-append mode, creating it if it does not exist. |
ax+ | Similar to 'a+', but if the file path exists, file reading and appending will fail. |
The above is the detailed content of Parsing NodeJs's fs read, write, delete and move monitoring. For more information, please follow other related articles on the PHP Chinese website!