fs模块

/ NodeJs / 没有评论 / 334浏览

在NodeJs中如果有文件的IO操作那么只需要引入一个fs模块即可,并且fs模块中所有的方法都支持异步和同步两种。

fs.unlink("C:/Users/Jilinwula/Desktop/nodejs/bak.txt", (err) => {
if (err) throw err;
console.log("successfully deleted C:/Users/Jilinwula/Desktop/nodejs/bak.txt");
});
fs.unlinkSync("C:/Users/Jilinwula/Desktop/nodejs/bak.txt");
console.log("successfully deleted C:/Users/Jilinwula/Desktop/nodejs/bak.txt");
fs.appendFile("C:/Users/Jilinwula/Desktop/nodejs/bak.txt", "吉林乌拉", (err) => {
if (err) throw err;
});
fs.mkdirSync("C:/Users/Jilinwula/Desktop/nodejs/bak/");
fs.readdirSync("C:/Users/Jilinwula/Desktop/");
fs.readFile("C:/Users/Jilinwula/Desktop/nodejs/bak.txt","utf8", (err, data) => {
if (err) throw err;
console.log(data);
});
fs.writeFile("C:/Users/Jilinwula/Desktop/nodejs/bak.txt","jilinwula.com","utf8", (err) => {
if (err) throw err;
});
fs.stat("C:/Users/Jilinwula/Desktop/nodejs/bak.txt", (err,stat) => {
if (err) throw err;
console.log(stat);
});
Stats {
  dev: 1448841754,
  mode: 33206,
  nlink: 1,
  uid: 0,
  gid: 0,
  rdev: 0,
  blksize: undefined,
  ino: 8444249301947359,
  size: 13,
  blocks: undefined,
  atime: 2017-04-06T07:36:14.029Z,
  mtime: 2017-04-06T07:53:34.061Z,
  ctime: 2017-04-06T07:53:34.061Z,
  birthtime: 2017-04-06T07:36:14.029Z }
atime "访问时间"
mtime "修改时间"
ctime "变化时间"
birthtime "创建时间"