This article mainly introduces node. fs. description of the write method. This article introduces the fs. write method description, syntax, receive parameters, use instances, and implement Source Code. For more information, see
Method description:
Writing a file (based on the file descriptor) is similar to fs. writeFile (), but this method provides more underlying operations. We recommend that you use more fs. writeFile () in actual applications ().
This method has two forms:
1. fs. write (fd, buffer, offset, length [, position], [callback (err, bytesWritten, buffer)])
This method writes the buffer into the file (find the file based on the file descriptor fd ).
2. fs. write (fd, data [, position [, encoding], [callback (err, written, string)])
This method writes data to a file (find the file based on the file descriptor fd ). If the data is not a buffer, the Instance value is forcibly converted into a string.
Syntax:
The Code is as follows:
Fs. write (fd, buffer, offset, length [, position], [callback (err, bytesWritten, buffer)])
Fs. write (fd, data [, position [, encoding], [callback (err, written, string)])
Because this method belongs to the fs module, we need to introduce the fs module (var fs = require ("fs") before use "))
Receiving parameters:
Fd file descriptor.
Buffer, data will be written. The buffer size is preferably a multiple of 8, which is more efficient.
Offset buffer write offset
Length (integer) specifies the length of the number of bytes that the file reads.
Position (integer) specifies the start position of File Reading. If this parameter is set to null, data is read from the current file pointer.
The callback passes three parameters: err, bytesRead, and buffer.
· Err exception
· BytesRead: number of bytes read
· Buffer: buffer object
Second form:
Encoding character encoding
Callback
· Err exception
· Written specifies the number of characters to be written to the file.
· Buffer returned by string
Example:
The Code is as follows:
// Fs. write (fd, buffer, offset, length [, position], [callback (err, bytesWritten, buffer)])
// Execution result: bytesWritten = 8, buffer =
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 =
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:
The Code 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 );
};