Method description:
Writing to a file (according to file descriptors), the function is similar to fs.writeFile(), but this method provides a more underlying operation. It is recommended to use multiple fs.writeFile() in actual applications.
This method comes in two forms:
1. fs.write(fd, buffer, offset, length[, position], [callback(err, bytesWritten, buffer)])
This way of writing writes buffer to a file (find the file according to the file descriptor fd).
2. fs.write(fd, data[, position[, encoding]], [callback(err, written, string)])
This way of writing writes data to a file (find the file according to the file descriptor fd). If the data is not an instance value of a buffer, it will be cast to a string.
grammar:
The code copy is as follows:
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, it is necessary to introduce the FS module before use (var fs= require("fs") )
Receive parameters:
fd file descriptor.
buffer, data will be written. The buffer size setting is preferably multiple of 8, which is more efficient.
offset write to offset buffer
length (integer) Specifies the length of the file reading bytes
position (integer) Specifies the starting position for file reading. If this item is null, data will be read from the position of the current file pointer.
The callback passes three parameters, err, bytesRead, and buffer
・ err exception
・ bytesRead: the number of bytes read
・buffer:buffer object
The second form:
encoding character encoding
callback
・ err exception
・written Specifies how many characters will be written to the file.
・ string Returned Buffer
example:
The code copy is as follows:
//fs.write(fd, buffer, offset, length[, position], [callback(err, bytesWritten, buffer)])
//Execution result: 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 result: 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(writeten);
console.log(string);
fs.close(fd,function(err){
if(err){
throw err;
}
console.log('file closed');
})
})
})
Source code:
The code copy is as follows:
// 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);
};