1.Buffer
buffer
-
Cache: A cache is a temporary area of memory used to store the bytecode that needs to be computed
-
A Buffer looks very much like an array in structure. Its elements are two digits in hexadecimal
-
A Buffer is a hexadecimal bytecode, much like binary. Two segment codes represent one byte, one binary represents one bit, one byte is eight binary bits, and one hexadecimal is two to the fourth power
2.Buffer
The module
-
A Buffer object is a node interface that processes binary data. It is a node natively provided global object that can be used directly without requiring it
-
JavaScript is good with strings, but not so good with binary data, such as TCP data streams. The Buffer object is designed to solve this problem. It is a constructor that generates an instance that represents a chunk of memory allocated by the V8 engine. It is an array-like object whose members are integer values from 0 to 255, i.e., an 8-bit byte
-
NodeJS is a server that must use binary data when processing things like TCP(network) streams or file streams (also called byte streams). Therefore, a Buffer class has been added to NodeJs to create a cache dedicated to binary data
-
In Node. js, utF-8 encoding is used by default, and a Chinese character usually contains 3 bytes
// Generate a 256-byte Buffer instance var bytes = new Buffer(256) // This method is deprecated and needs to be buffer.alloc () instead. var buf = Buffer.alloc(256) Copy the code
-
The Buffer constructor can generate an instance with New and can take many forms of arguments
var str = 'Hello wrold' // Save a string to buffer var buf = Buffer.from(str) console.log(buf) // Note that the from method argument cannot be a number // The type can be string, Buffer, ArrayBuffer, Array, or array-like Object var buff = Buffer.from(buf)// copy to create space directly in memory Copy the code
-
Access to a Buffer
*/ var buf3 = buffer. alloc(10)// Create a 20 byte memory space for(let I = 0; i<buf3.length; i++){ buf3[i] = i; Console. log("Buffer contents ",buf3[I].toString()); }Copy the code
-
Conversion of Buffer values
Buffer object and string conversion, need to specify the encoding format, default UTF-8 format
Buf.tostring ([encoding[,start[,end]]]) encoding Optional, The default encoding format is UTF-8. Start (optional) Start (default) End (optional) End (default) */ let buf4 = Buffer.from('Hello') / / not to participate console.log(buf4.toString()) // Specify the encoding and the converted data, each Chinese is 3 bytes console.log(buf4.toString('utf8'.3.6)) 1.ascii 2.utf8 3.Utf16le: utf-816Small encoder, support greater than U+10000A four-byte character.4.Ucs2: alias of UTF16LE.5.base64 6.Hex: Converts each byte to two hexadecimal charactersCopy the code
- Buffers are used to process binary data streams
- A Buffer is a byte;
- Instances are like arrays of integers, but unlike arrays, once instantiated, the size is fixed
- Memory is not allocated by V8, it is physical memory allocated by C++ code outside the V8 heap
3.Buffer
The basic method of
-
Buffer.isencoding () : Returns a Boolean value indicating whether the Buffer instance is the specified encoding
-
Buffer.isbuffer () : Takes an object as an argument and returns a Boolean value indicating whether the object is an instance of Buffer
-
Buffer.bytelength () : Returns the actual length of the string in bytes. The default encoding is UTF-8
-
Buffer.concat() : Merges a group of Buffer objects into a single Buffer object
If buffer. concat’s argument array has only one member, that member is returned directly. If there are multiple members, a new Buffer object with multiple members is returned
You can also accept a second argument that specifies the total length of the merge and Buffer objects. If omitted, Node will calculate this value internally and then perform the merge from this value. Therefore, the display provides this parameter to speed up the operation
4.Buffer
Instance method of
- Write () method: can be used to specify
The first argument is what data is written to in the Buffer formation. The second argument (which can be omitted) is the position from which data is written (default starts at 0).
The third parameter (which can be omitted) is the encoding mode, which defaults to URF-8 - Slice () method: returns a slice from the original object at the specified location
Buffer
Example, whose two parameters are the start and end positions of the cut - ToString () : The toString() method converts the Buffer instance to a string in the specified encoding (utF8 by default). The toString method can return only the contents of memory at a specified location, with its second argument representing the start location and its third argument representing the end location, both starting at 0.
- ToJSON () : The toJSON method converts the Buffer instance into a JSON object. If the json.stringify method calls the Buffer instance, the toJSON method is called first by default.
- Buf.fill () : fills data
Buf.fill (value [, start, [, end]]) value Must: Initialize data start Position at which initialization starts End position at which initialization endsCopy the code
- Buf.copy () : copies content from the target BUF
Buffer. Copy (target buf, write buf start position, copy buffer start position, Let buf = buffer. From (' hello ') let buf2 = buffer. From ([0,0,0,0,0]) // copy from subscript 3 to subscript 6 to buf2 Copy (buf2,1, 3,6) from the first byte of buf2Copy the code
5.Buffer
Instance properties of
- Length: The length attribute is returned
Buffer
The length of memory occupied by an object, independent of the object’s contents - No matter what I write, the length property always returns, okay
Buffer
Object, which you can pass in if you want to know how many bytes a string takes upBuffer. ByteLength method
- The length attribute is writable, but this results in undefined behavior and is not recommended if you want to modify it
Buffer
Object length. It is recommended to return a new one using the slice methodBuffer object
6. The fs module
-
Fs is short for Filesystem. This module provides read and write capabilities for local files and is basically a simple wrapper for POSIX file operation commands
-
Fs module is very rich in functions. In addition, the module provides asynchronous and synchronous options for almost all operations.
-
File reading mode
① The callback function is triggered only after all the contents on the hard disk are read into the content, also known as direct read
(2) Streaming reading: the data is read from the hard disk will trigger a callback function, that is, read a section of data processing a section of data, to achieve large file operation
③ Direct reading is divided into synchronous reading and asynchronous reading
-
The readFile method is used to read data asynchronously, using a callback function to accept what was read
The first argument to the readFile method is the path to the file, which can be either absolute or relative. Note that if the path is relative, it is relative to the path where the current process is (process.cwd()), not relative to the path where the current script is.
The second argument to the readFile method is the callback function after reading. The first argument to this function is the error object when the error occurred, and the second argument is a Buffer instance representing the contents of the file.
// Read the file directly -- asynchronous operation var fs = require('fs'); fs.readFile("./src/demo.txt".function (err, data) { console.log(data) // err Specifies an error object. If there is an error, it has a value. If there is no error, it is null // Data is a stream of Buffer data, if needed to display data via data.tostring }) // The second argument is passed in the format of the data, so data will display the data directly fs.readFile('./src/demo.txt'.'utf-8'.function(error,data){ console.log(data); }) Copy the code
-
The readFileSync method is used to read data synchronously, using assignment to read data
The first argument to the readFileSync method is the file path. The second argument can be a configuration object or a string representing the encoding of a text file. The default configuration object is {encoding: null, flag: ‘r’}, that is, the file encoding is null and the read mode is r (read-only) by default. The readFileSync method returns a Buffer instance if the second argument does not specify encoding, otherwise it returns a string.
Fs. readFileSync(file path) var data = fs.readFileSync('./src/demo.txt') console.log(data.toString()); Copy the code
-
The synchronous version does not receive an error. If there is an error in the synchronization, the system will automatically report an error, so the asynchronous method, if there is an error, the system will not report an error, and will pass the error to the callback itself
-
The writeFile method is used to write files asynchronously. (Default direct overwrite)
// Write files asynchronously / / fs. WriteFile (' name 'and' data ', function (err) {/ * data to failed callback function * /}) // The encoding of the written character can be passed in. If it is a Buffer, it can not be specified var datas = "
This is a file written through the FS module
" fs.writeFile('index.html',datas,function(error){ console.log(error); }) fs.writeFile('demo.txt'.'utf-8',datas,function(error){ console.log(error); }) Copy the codefs.writeFile(file, data[, options], callback)
1. The file < string > | < Buffer > | < URL > | < integer > filename or file descriptors.
2. Data < string > | < Buffer > | < TypedArray > | < DataView > written data
3. The options < Object > | < string > write data parameters
1) encoding < string > | < null > data encoding format: default value: ‘utf8’.
② Mode < INTEGER > Default value: 0O666.
③flag
See supported file system flags. Default value: ‘w’.
4. Callback
1) err < Error >
-
Append content
//fs.appendFile(file path, data, callback) var data = "
I wrote the file via fs module
" fs.appendFile("./test.html",data,(err) = > { console.log(err) }) Copy the code -
Stat Reads file/folder information. Stats File information object
fs.stat("File name".function(err,stats){ Stats is a file information object that contains information about common files }) // Read synchronously const stat = fs.statSync("./node") var fs = require("fs"); fs.stat("index.json".function (err, stats) { console.log(stats) }) Dev: 226428394, mode: 33206, nlink: 1, uid: 0, gid: 0, rdev: 0, blksize: undefined, ino: 10414574138374020, size: 17, // File size: 17 blocks: undefined, atimeMs: 1566988486138.9304, // mtimeMs: BirthtimeMs: 1566625311439.442, ctimeMs: 1566625311439.442, birthtimeMs: 1566625188201.4536, atime: 2019-08-28T10:34:46.139z, // Last file access time mtime: 2019-08-24T05:41:51.439z, // File content modification time ctime: 2019-08-24T05:41:51.439z, // Time of file state change birthTime: 2019-08-24T05:39:48.201z // Time of first creation}*/ Copy the code
-
Stats object method
Stats.isfile () checks whether it is a file stats.isdirectory () checks whether it is a folder.
var fs = require("fs"); fs.stat("index.json".function (err, state) { console.log(stats.isFile()) }) Copy the code
-
Rename Changes the file name.
var fs = require("fs"); fs.rename("./text", 'test.txt', err => { if(err) throw err console.log("done!") }) Copy the code
-
exists(path, callback)
The exists method is used to determine whether a given path exists and then, regardless of the result, calls a callback function that takes a Boolean value to indicate whether a file exists.
-
Mkdir ()/readdir()/fs.rmdir() Add a directory and read the list of files in the directory and delete empty folders
The mkdir method is used to create a directory. It takes three arguments, the first a directory name, the second a permission value, and the third a callback function.
The readdir method reads a directory and returns an array of contained files and subdirectory names.
Process. CWD is the directory where the current process resides
fs.mkdir("./src".function(err){ // Create a failed callback }) fs.readdir("./src".function(err,list){ console.log(err); console.log(list) // list reads the folder list }) // List is an array of all files and folders in the Node folder, if folders exist // If the folder does not exist, err is used Copy the code
-
watchfile/unwatchfile()
The WatchFile method listens for a file and automatically triggers the callback function if the file changes.
The file changes here are divided into code modification files and manual modification files
The unwatchfile method is used to unlisten a file.
// watch is to listen for all file changes in the folder fs.watch(". /", {recursive: false // The default is whether flase listens recursively }, (changeType, filename) = > { console.log(chaneType) // The type of change, whether to modify a file, add a file, or delete a file console.log(filename) // The name of the modified file }) // watchFile listens for changes to a file fs.watchFile('./node/1.txt'.(c,p) = > { console.log(p); console.log(c) }) Copy the code
P listens for stats before the file changes
C listens for stats after the file has changed (that is, the current file)
7. Synchronous and asynchronous
-
The synchronization operation blocks subsequent code, which must wait for the synchronization operation to complete. The asynchronous operation executes the following code first, and when the asynchronous operation completes, the callback function is executed
-
The result of the synchronization operation is assigned directly to the variable as a value
-
Asynchronous operations must have a callback function whose first argument is an error object
-
Most nodes have a synchronous version of the asynchronous API called asynchronous API +Sync
8. File system id
When the flag option is a string, the following flags can be used:
-
‘A’ – Opens the file for append. If the file does not exist, it is created.
-
‘ax’ – similar to ‘a’, but fails if the path already exists.
-
‘A +’ – Opens files for reading and append. If the file does not exist, it is created.
-
‘ax+’ – similar to ‘a+’, but fails if the path already exists.
-
‘as’ – Opens the file in synchronous mode for appending. If the file does not exist, it is created.
-
‘as+’ – Opens files in synchronous mode for reading and appending. If the file does not exist, it is created.
-
‘r’ – Opens the file for reading. If the file does not exist, an exception occurs.
-
‘r+’ – Opens the file for reading and writing. If the file does not exist, an exception occurs.
-
‘RS +’ – Opens files in synchronous mode for reading and writing. Instructs the operating system to bypass the local file system cache.
-
This is useful for opening files on NFS mounts because it allows skipping local caches that may be outdated. It has a very real impact on I/O performance, so it is not recommended to use this flag unless required.
-
. This will not convert fs.open() or fsPromises. Open () to a synchronous blocking call. If synchronous operations are required, something like fs.opensync () should be used.
-
.’w’ – Opens the file for writing. Create a file if it does not exist, or truncate it if it already exists.
-
.’wx’ – Similar to ‘w’, but fails if the path already exists.
-
.’w+’ – Opens files for reading and writing. Create a file if it does not exist, or truncate it if it already exists.
-
.’wx+’ – Similar to ‘w+’, but fails if the path already exists.
9. Fs Read/write flow mode
-
Using the read method, the file is read into the cache as a whole. Using the write method, the file is read into the cache as a whole and written to the file as a whole after modification.
-
Both read and write in Node treat the file as a whole. Node needs to allocate the same size of cache space in the memory as the file size. The file size may exceed the memory size.
-
Stream: In an application, a stream is an ordered transfer of bytes of data with a starting and ending point. When communicating and transmitting data between various objects in the application program, the data contained in the object is always converted into various forms of stream data (byte data), and then through the stream transmission, the stream data is converted into usable data in the object after reaching the destination object
-
All data transmitted over the Internet is transmitted as a stream, which means it is transmitted in a single section
10. Flow operations
-
Read the file as a stream
// 1. Create a read stream var stream = fs.createReadStream(path) // 2. Bind the data event to accept data stream.on("data".function(data){ console.log(data) console.log(1) // Print several times, indicating that the data is divided into several streams }) // 3. Bind the end event to indicate that the reading is complete stream.on("end".function(){ console.log("Data stream read completed.")})// 4. Bind the error event stream.on("error".function(err){ throw err }) Copy the code
var fs = require("fs"); // Create a readable stream var stream = fs.createReadStream("./file2.txt"); // Listen for the data event and execute the callback function every time a stream is read stream.on("data".function (data) { console.log(data) }) Copy the code
The length of each data stream is 65536 bytes, and the size is 65536/1024= 64KB, that is, the size of each data stream is 64KB, which can be viewed by data.length
Read stream events: End indicates the event triggered after the read stream finishes reading, and error indicates the time triggered after the read stream reads incorrectly
stream.on("end".function () { console.log("Read complete")})Copy the code
-
Write files as streams
// 1. Create a write flow var stream = fs.createWriteStream(path) // 2. Write data stream.write("Data 1") // 3. Write to the stream after the data is written stream.end() // 4. Data write is complete stream.on("finish".function(){}) // 5. Bind the error event stream.on("error".function(err){}) Copy the code
var fs = require("fs"); // Create a write stream var stream = fs.createWriteStream("./file3.txt"); // Write data stream.write("The bread is gone."); stream.write("I ran out of milk."); stream.write("I've run out of money."); stream.write("I lost my job."); stream.write("The worst part is I got lost."); stream.end(); // End of the declaration that data must be displayed in streamed mode // The write stream can handle what needs to be done after the write stream is complete by listening to the Finish event stream.on("finish".function () { console.log("Write complete")})// Write the stream error event,error, which triggers a callback when writing an error stream.on("error".function (err) { console.log(err) }) Copy the code
10. Pipe and chain flow