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('成功') })File monitoring (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 |
---|---|
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!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function