方法说明:
写入文件(根据文件描述符),功能与 fs.writeFile() 类似,但该方法提供更底层的操作,实际应用中建议使用多 fs.writeFile() 。
该方法有两种形式:
1、fs.write(fd, buffer, offset, length[, position], [callback(err, bytesWritten, buffer)])
这种写法将buffer写入文件(根据文件描述符fd来查找文件)。
2、fs.write(fd, data[, position[, encoding]], [callback(err, written, string)])
这种写法将数据data写入文件(根据文件描述符fd来查找文件)。如果数据不是一个缓冲区的实例值将被强制转换为一个字符串。
语法:
fs.write(fd, buffer, offset, length[, position], [callback(err, bytesWritten, buffer)])
fs.write(fd, data[, position[, encoding]], [callback(err, written, string)])
由于该方法属于fs模块,使用前需要引入fs模块(var fs= require(“fs”) )
接收参数:
fd 文件描述符。
buffer 缓冲区,数据将被写入。buffer尺寸的大小设置最好是8的倍数,效率较高。
offset buffer写入的偏移量
length (integer) 指定文件读取字节数长度
position (integer) 指定文件读取的起始位置,如果该项为null,将从当前文件指针的位置开始读取数据。
callback 回调传递了三个参数,err, bytesRead, buffer
? err 异常
? bytesRead:读取的字节数
? buffer:缓冲区对象
第二种形式:
encoding 字符编码
callback
? err 异常
? written 指定多少字符数将被写入到文件。
? string 返回的Buffer
例子:
//fs.write(fd, buffer, offset, length[, position], [callback(err, bytesWritten, buffer)])
//执行结果: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)])
//执行结果: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');
})
})
})
源码:
// 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);
};