阅读 87

Node.js文件模块API整理

本文为node.js学习笔记,不完全罗列fs的所有API。复制于Node.js开发指南4.4章,目的是为了自己方便查阅.API文档地址,如有不足之处,欢迎指出,一起学习呀。

功能异步函数同步函数
打开文件fs.open(path,flags, [mode], [callback(err,fd)])fs.openSync(path, flags, [mode])
关闭文件fs.close(fd, [callback(err)])fs.closeSync(fd)
读取文件(文件描述符)fs.read(fd,buffer,offset,length,position,[callback(err, bytesRead, buffer)])fs.readSync(fd, buffer, offset,length, position)
写入文件(文件描述符)fs.write(fd,buffer,offset,length,position,[callback(err, bytesWritten, buffer)])fs.writeSync(fd, buffer, offset,length, position)
读取文件内容fs.readFile(filename,[encoding],[callback(err, data)])fs.readFileSync(filename,[encoding])
写入文件内容fs.writeFile(filename, data,[encoding],[callback(err)])fs.writeFileSync(filename, data,[encoding])
删除文件fs.unlink(path, [callback(err)])fs.unlinkSync(path)
创建目录fs.mkdir(path, [mode], [callback(err)])fs.mkdirSync(path, [mode])
删除目录fs.rmdir(path, [callback(err)])fs.rmdirSync(path)
读取目录fs.readdir(path, [callback(err, files)])fs.readdirSync(path)
获取真实路径fs.realpath(path, [callback(err,resolvedPath)])fs.realpathSync(path)
重命名fs.rename(path1, path2, [callback(err)])fs.renameSync(path1, path2)
截断fs.truncate(fd, len, [callback(err)])fs.truncateSync(fd, len)
更改所有权fs.chown(path, uid, gid, [callback(err)])fs.chownSync(path, uid, gid)
更改所有权(文件描述符)fs.fchown(fd, uid, gid, [callback(err)])fs.fchownSync(fd, uid, gid)
更改所有权(不解析符号链接)fs.lchown(path, uid, gid, [callback(err)])fs.lchownSync(path, uid, gid)
更改权限fs.chmod(path, mode, [callback(err)])fs.chmodSync(path, mode)
更改权限(文件描述符)fs.fchmod(fd, mode, [callback(err)])fs.fchmodSync(fd, mode)
更改权限(不解析符号链接)fs.lchmod(path, mode, [callback(err)])fs.lchmodSync(path, mode)
获取文件信息fs.stat(path, [callback(err, stats)])fs.statSync(path)
获取文件信息(文件描述符)fs.fstat(fd, [callback(err, stats)])fs.fstatSync(fd)
获取文件信息(不解析符号链接)fs.lstat(path, [callback(err, stats)])fs.lstatSync(path)
创建硬链接fs.link(srcpath, dstpath, [callback(err)])fs.linkSync(srcpath, dstpath)
创建符号链接fs.symlink(linkdata, path, [type],[callback(err)])fs.symlinkSync(linkdata, path,[type])
读取链接fs.readlink(path, [callback(err,linkString)])fs.readlinkSync(path)
修改文件时间戳fs.utimes(path, atime, mtime, [callback(err)])fs.utimesSync(path, atime, mtime)
修改文件时间戳
(文件描述符)fs.futimes(fd, atime, mtime, [callback(err)])fs.futimesSync(fd, atime, mtime)
同步磁盘缓存fs.fsync(fd, [callback(err)])fs.fsyncSync(fd)
文章分类
前端
文章标签