Instructions for the fs.write method in node.js
- 2020-05-05 10:51:41
- OfStack
method description:
Writing to a file (according to the file descriptor) is similar in function to fs.writeFile (), but this method provides lower-level operations, and in practice it is recommended to use more fs.writeFile () .
There are two forms of this method:
1. fs.write(fd, buffer, offset, length[, position], [callback(err, bytesWritten, buffer)])
This writes buffer to a file (find the file by the file descriptor fd).
2. fs.write(fd, data[, position[, encoding]], [callback(err, written, string)])
This writes the data data to a file (find the file by the file descriptor fd). If the data is not an instance value of a buffer, it is cast to a string.
syntax:
fs.write(fd, buffer, offset, length[, position], [callback(err, bytesWritten, buffer)])
fs.write(fd, data[, position[, encoding]], [callback(err, written, string)])
Since this method belongs to the fs module, the fs module (var fs= require(" fs "))
needs to be introduced before usereceive parameters:
fd file descriptor.
buffer buffer, data will be written. The size setting of buffer size is preferably a multiple of 8 for high efficiency.
offset buffer write offset
length (integer)
position (integer) specifies the starting position for the file to be read, and if the entry is null, the data will be read from the position of the current file pointer.
The callback callback passes three parameters, err, bytesRead, buffer
· err exception
· bytesRead: number of bytes read
· buffer: buffer object
second form:
encoding character encoding
callback
· err exception
· written specifies how many characters will be written to the file.
· string returns Buffer
example:
//fs.write(fd, buffer, offset, length[, position], [callback(err, bytesWritten, buffer)])
// Execution results: bytesWritten = 8 , buffer = <Buffer 00 00 00 01 00 00 00 00>
var fs = require('fs');
fs.open('content.txt', 'a', function(err,fd){
if(err){
throw err;
}
var data = '123123123 hello world';
var buf = new Buffer(8);
fs.write(fd, buf, 0, 8, 0, function(err, bytesWritten, buffer){
if(err){
throw err;
}
console.log(bytesWritten);
console.log(buffer);
fs.close(fd,function(err){
if(err){
throw err;
}
console.log('file closed');
})
})
})
//fs.write(fd, data[, position[, encoding]], [callback(err, written, string)])
// Execution results: written = 21 , string = <Buffer 31 32 33 31 32 33 31 32 33 20 68 65 6c 6c 6f 20 77 bf 72 6c 64>
var fs = require('fs');
fs.open('content.txt', 'a', function(err,fd){
if(err){
throw err;
}
var data = '123123123 hello world';
fs.write(fd, data, 0 , 'utf-8', function(err, written, string){
if(err){
throw err;
}
console.log(written);
console.log(string);
fs.close(fd,function(err){
if(err){
throw err;
}
console.log('file closed');
})
})
})
source code:
// usage:
// fs.write(fd, buffer, offset, length[, position], callback);
// OR
// fs.write(fd, string[, position[, encoding]], callback);
fs.write = function(fd, buffer, offset, length, position, callback) {
if (util.isBuffer(buffer)) {
// if no position is passed then assume null
if (util.isFunction(position)) {
callback = position;
position = null;
}
callback = maybeCallback(callback);
var wrapper = function(err, written) {
// Retain a reference to buffer so that it can't be GC'ed too soon.
callback(err, written || 0, buffer);
};
return binding.writeBuffer(fd, buffer, offset, length, position, wrapper);
}
if (util.isString(buffer))
buffer += '';
if (!util.isFunction(position)) {
if (util.isFunction(offset)) {
position = offset;
offset = null;
} else {
position = length;
}
length = 'utf8';
}
callback = maybeCallback(position);
position = function(err, written) {
// retain reference to string in case it's external
callback(err, written || 0, buffer);
};
return binding.writeString(fd, buffer, offset, length, position);
};