2016年3月9日
nodejs
fs模块
fs.open(path, flags, [mode], callback)
异步打开一个文件
path : 要打开的文件的路径
flags : 打开文件的方式 读/写
mode : 设置文件的模式 读/写/执行 4/2/1(可以不填,意义不大))
callback : 回调
err : 文件打开失败的错误保存在err里面,如果成功err为null
fd : 被打开文件的标识,和定时器
fs.open('1.txt', 'r', function(err, fd) {
//console.log(err);
//console.log(fd);
if (err) {
console.log( '文件打开失败' );
} else {
console.log( '文件打开成功' );
console.log( fd );
}
});
//结果:
null
3
文件打开成功
3
fs.open('1.txt', 'r', function(err, fd) {
console.log(fd);
});
console.log("hello");
//结果
hello
3
//两个操作独自运行,打开文件需要一定时间,所以先输出"hello"。
fs.openSync(path, flags, [mode])
fs.open() 的同步版.
fs.read(fd, buffer, offset, length, position, callback)
从指定的文档标识符fd读取文件数据。
fd : 通过open方法成功打开一个文件返回的编号
buffer : buffer对象
offset : 新的内容添加到buffer中的起始位置
length : 添加到buffer中内容的长度
position :读取的文件中的起始位置
callback : 回调
err
buffer的长度
buffer对象
Last updated
Was this helpful?