每天撸个API -- File System (3)

realpath : 真实路径

fs.realpath(path, [cache], callback)

var cache = {'/example': '/home/yofine/example'};

//Asynchronous realpath
fs.realpath('/example/demo1_s', cache, function (err, resolvedPath) {
  if (err) {
    throw err;
  }
  console.log(resolvedPath);
});

log :

/home/yofine/example/demo1.txt

fs.realpathSync(path, [cache])

var cache = {'/example': '/home/yofine/example'};

//Synchronous realpath
var resolvedPath = fs.realpathSync('/example/demo1_s', cache);
console.log(resolvedPath);

log :

/home/yofine/example/demo1.txt

unlink : 删除文件链接

fs.unlink(path, callback)

//Asynchronous unlink
fs.unlink('/path/demo2.txt', function (err) {
  if (err) {
    throw err;
  }
  console.log('unlink complete');
});

fs.unlinkSync(path)

//Synchronous unlink
fs.unlinkSync('/path/demo2.txt');

rmdir : 删除空目录

fs.rmdir(path, callback)

//Asynchronous rmdir
fs.rmdir('/path/demo_dir', function (err) {
  if (err) {
    throw err;
  }
  console.log('rmdir complete');
});

fs.rmdirSync(path)

//Synchronous rmdir
fs.rmdirSync('/path/demo_dir');

mkdir : 创建目录

fs.mkdir(path, [mode], callback)

//Asynchronous mkdir
fs.mkdir('/path/demo_dir', function (err) {
  if (err) {
    throw err;
  }
  console.log('mkdir complete');
});

fs.mkdirSync(path, [mode])

//Synchronous nkdir
fs.mkdir('/path/demo_dir');

[mode] 默认值为 0777

readdir : 读取目录

fs.readdir(path, callback)

//Asynchronous readdir
fs.readdir('/path/example', function (err, files) {
  if (err) {
    throw err;
  }
  console.log(files);
});

fs.readdirSync(path)

//Synchronous readdir
var files = fs.readdirSync('/path/example');
console.log(files);

log :

[ 'demo1.txt', 'demo1_h', 'demo1_s', 'demo_dir' ]  

open & close : 打开/关闭文件

fs.open(path, flags, [mode], callback)

fs.close(fd, callback)

//Asynchronus open&close
fs.open('/path/demo1.txt', 'a', function (err, fd) {
  if (err) {
    throw err;
  }
  console.log(fd);
  fs.close(fd, function () {
    console.log('Async Done');
  });
});

fs.openSync(path, flags, [mode])

fs.closeSync(fd)

//Synchronous open&close
var fd = fs.openSync('/path/demo1.txt', 'a');
console.log(fd);
fs.closeSync(fd);
console.log('Sync Done');

utimes : 修改时间戳(path)

fs.utimes(path, atime, mtime, callback)

//Asynchronous utimes
fs.utimes('/path/demo1.txt', 1388648321, 1388648321, function (err) {
  if (err) {
    throw err;
  }
  console.log('utime complete');
});

fs.utimesSync(path, atime, mtime)

//Synchronous utimes
fs.utimesSync('/path/demo1.txt', 1388648321, 1388648321);

futimes : 更改时间戳(fd)

fs.futimes(fd, atime, mtime, callback)

//Asynchronous futimes
fs.open('/path/demo1.txt', 'a', function (err, fd) {
  if (err) {
    throw err;
  }
  fs.futimes(fd, 1388648322, 1388648322, function (err) {
    if (err) {
      throw err;
    }
    console.log('futimes complete');
    fs.close(fd, function () {
      console.log('Done');
    });
  });
});

fs.futimesSync(fd, atime, mtime)

//Synchronous futimes
var fd = fs.openSync('/path/demo1.txt', 'a');
fs.futimesSync(fd, 1388648322, 1388648322);
fs.closeSync(fd);
console.log('Done');
    原文作者:Yofine
    原文地址: https://segmentfault.com/a/1190000000375704
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞