1.****nodejs文件操作分为同步和异步请求。同步后面加了Sync //异步删除 fs.unlink('/tmp/hello', (err) => { if (err) throw err; console.log('successfully deleted /tmp/hello'); }); //同步删除 fs.unlinkSync('/tmp/hello'); console.log('successfully deleted /tmp/hello');2.**异步不保证顺序 fs.rename('/tmp/hello', '/tmp/world', (err) => { if (err) throw err; console.log('renamed complete'); }); fs.stat('/tmp/world', (err, stats) => { if (err) throw err; console.log(`stats: ${JSON.stringify(stats)}`); //stats: {"dev":-593730986,"mode":33206,"nlink":1,"uid":0,"gid":0, //"rdev":0,"ino":2251799813714667,"size":3,"atime":"2016-03-25T07:41:15.892Z", //"mtime":"2016-03-25T07:41:19.870Z","ctime":"2016-03-25T07:42:00.065Z", //"birthtime":"2016-03-25T07:41:15.819Z"} }); //下面可以保证 fs.rename('/tmp/hello', '/tmp/world', (err) => { if (err) throw err; fs.stat('/tmp/world', (err, stats) => { if (err) throw err; console.log(`stats: ${JSON.stringify(stats)}`); }); });4.Class: fs.FSWatcher 类, 从fs.watch()方法返回 (1)Event: 'change' event 文件类型改变 filename 文件名改变 (2)Event: 'error' (3)watcher.close() 停止监听文件变化5.Class: fs.ReadStream 类 (1)Event: 'open' fd 整数文件描述符 当ReadStream文件打开时触发 (2)readStream.path 打开文件的路径6.***Class: fs.Stats 类 (1)对象从fs.stat(), fs.lstat()、 fs.fstat() stats.isFile() stats.isDirectory() stats.isBlockDevice() stats.isCharacterDevice() stats.isSymbolicLink() (only valid with fs.lstat()) stats.isFIFO() stats.isSocket() //例子 fs.stat('input.txt', function (err, stats) { if (err) { return console.error(err); } console.log(stats); console.log("读取文件信息成功!"); // 检测文件类型 console.log("是否为文件(isFile) ? " + stats.isFile()); console.log("是否为目录(isDirectory) ? " + stats.isDirectory()); }); (2)对于普通文件 util.inspect(stats)会返回 { dev: 2114, ino: 48064969, mode: 33188, nlink: 1, uid: 85, gid: 100, rdev: 0, size: 527, blksize: 4096, blocks: 8, atime: Mon, 10 Oct 2011 23:24:11 GMT, //Access Time" mtime: Mon, 10 Oct 2011 23:24:11 GMT, //Modified Time 数据改变 ctime: Mon, 10 Oct 2011 23:24:11 GMT, //Change Time status 改变 birthtime: Mon, 10 Oct 2011 23:24:11 GMT //创建日期 }7.Class: fs.WriteStream 文件写入流 (1)Event: 'open' fd 整数文件描述符 当WriteStream文件打开时触发 (2)writeStream.bytesWritten 当现在为止已经写入的文件大小 (3)writeStream.path 写入的路径8.fs.access(path[, mode], callback) (1)mode类型 fs.F_OK - 确定文件是否存在,默认,没有rwx权限 fs.R_OK - 文件可读 fs.W_OK - 文件可写 fs.X_OK - 文件可执行 对于 Windows 没作用(will behave like fs.F_OK). (2)例子 fs.access('/etc/passwd', fs.R_OK | fs.W_OK, (err) => { console.log(err ? 'no access!' : 'can read/write'); }); (3)同步,失败会抛出异常 fs.accessSync(path[, mode])9.***fs.appendFile(file, data[, options], callback) 添加文件 (1)参数 file | filename 或者 file descriptor data | 数据 options