Description of Fs.writesync method used in Node.js _node.js

Source: Internet
Author: User
Tags require

Method Description:

Sync version of Fs.write (). Writes a file (according to the file descriptor).

Grammar:

Copy Code code as follows:

Fs.writesync (fd, buffer, offset, length[, position])

Fs.writesync (FD, data[, position[, encoding])

Because this method belongs to the FS module, it is necessary to introduce FS module (VAR fs= require ("FS") before use.

Receive parameters:

FD file descriptor.

Buffer buffers, the data will be written. The size of the buffer size is set preferably at a multiple of 8, with high efficiency.

Offsets to offset buffer writes

Length (integer) specifies how long the file reads bytes

Position (integer) specifies the starting position of the file read and, if it is null, reads the data from the position of the current file pointer.

Encoding (String) character encoding

Example:

Copy Code code as follows:

Fs.writesync (fd, buffer, offset, length[, position])
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.writesync (FD, buf, 0, 8, 0);
Fs.close (Fd,function (err) {
if (err) {
throw err;
}
Console.log (' file closed ');
})
})
Fs.writesync (FD, data[, position[, encoding])
var fs = require (' FS ');
Fs.open (' Content.txt ', ' a ', function (ERR,FD) {
if (err) {
throw err;
}

var data = ' 123123123 Hello world ';
Fs.writesync (FD, data, 0, ' utf-8 ');

Fs.close (Fd,function (err) {
if (err) {
throw err;
}
Console.log (' file closed ');
})
})

Source:

Copy Code code as follows:

Usage
Fs.writesync (fd, buffer, offset, length[, position]);
OR
Fs.writesync (FD, string[, position[, encoding]);
Fs.writesync = function (fd, buffer, offset, length, position) {
if (util.isbuffer (buffer)) {
if (util.isundefined (position))
Position = NULL;
Return Binding.writebuffer (FD, buffer, offset, length, position);
}
if (!util.isstring (buffer))
Buffer + = ';
if (util.isundefined (offset))
offset = null;
Return binding.writestring (FD, buffer, offset, length, position);
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.