Home >Web Front-end >JS Tutorial >Instructions for using the fs.write method in node.js_node.js

Instructions for using the fs.write method in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 16:27:081646browse

Method description:

Write to a file (according to the file descriptor). The function is similar to fs.writeFile(), but this method provides lower-level operations. In actual applications, it is recommended to use more fs.writeFile() .

This method has two forms:

1. fs.write(fd, buffer, offset, length[, position], [callback(err, bytesWritten, buffer)])

This way of writing writes the buffer to a file (finding the file based on the file descriptor fd).

2. fs.write(fd, data[, position[, encoding]], [callback(err, written, string)])

This way of writing writes data to a file (finding the file based on the file descriptor fd). If the data is not a buffer instance the value will be cast to a string.

Grammar:

Copy code 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)])

Since this method belongs to the fs module, the fs module needs to be introduced before use (var fs= require(“fs”) )

Receive parameters:

fd File descriptor.

buffer The buffer into which data will be written. It is best to set the buffer size to a multiple of 8, which is more efficient.

offset The offset written to the buffer

length (integer) Specifies the length of the file read in bytes

position (integer) Specifies the starting position of file reading. If this item is null, data will be read starting from the position of the current file pointer.

callback The 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 Returned Buffer

Example:

Copy code 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:

Copy 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);
};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn