Parsing NodeJs's fs read, write, delete and move monitoring
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'
- callback {Function}
//写入文件 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:
- path: file/file path
- flags: file opening behavior
- mode: Set the file mode (permissions). The default permissions for file creation are 0666 (readable, writable).
- callback: callback function
//打开文件 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:- filename:{String}
- data:{String | Buffer}
- options :{Object}
mode {Number} Default = 438 (aka 0666 in Octal)
flag {String} default = 'a'
- ##callback {Function}
//给文件添加数据 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application

Node.js and Java each have their pros and cons in web development, and the choice depends on project requirements. Node.js excels in real-time applications, rapid development, and microservices architecture, while Java excels in enterprise-grade support, performance, and security.
