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

fsync : 同步

fs.fsync(fd, callback)

//Asynchronous fsync
fs.open('/path/demo2', 'a', function(err, fd) {
  if (err) throw err;
  fs.fsync(fd, function(err) {
    if (err) throw err;
    fs.close(fd, function(err) {
      if (err) throw err;
      console.log('Complete!')
    });
  });
});

fs.fsyncSync(fd)

//Synchronous fsync
var fd = fs.openSync('/path/demo2', 'a');
fs.fsyncSync(fd);
fs.closeSync(fd);

fs.fsync is just an asynchronous node wrapper for unix’s fsync

write : 写入

var buffer = new Buffer('yofine');

fs.write(fd, buffer, offset, length, position, callback)

//Asynchronous write
fs.open('/path/demo1.txt', 'a', function(err, fd) {
  if (err) throw err;
  fs.write(fd, buffer, 0, buffer.length, null, function(err, written, buffer) {
    if (err) throw err;
    console.log( written + 'bytes were written from buffer');
    fs.close(fd, function(err) {
      if (err) throw err;
      console.log('Complete');
    });
  });
});

fs.writeSync(fd, buffer, offset, length, position)

//Synchronous write
var fd = fs.openSync('/path/demo1.txt', 'a');
var written = fs.writeSync(fd, buffer, 0, buffer.length, null);
console.log(written + 'bytes were written from buffer');
fs.closeSync(fd);

read : 读取

var buffer = new Buffer(100);

fs.read(fd, buffer, offset, length, position, callback)

//Asynchronous read
fs.open('/path/demo1.txt', 'r', function(err, fd) {
  if (err) throw err;
  fs.read(fd, buffer, 0, buffer.length, null, function(err, bytesRead, buffer) {
    if (err) throw err;
    console.log('bytesRead : ' + bytesRead);
    fs.close(fd, function(err) {
      console.log('Complete!');
    });
  });
});

fs.readSync(fd, buffer, offset, length, position)

//Synchronous read
var fd = fs.openSync('/path/demo1.txt', 'r');
var bytesRead =  fs.readSync(fd, buffer, 0, buffer.length, null);
console.log('bytesRead : ' + bytesRead);
fs.close(fd);

readFile : 读取文件

fs.readFile(filename, [options], callback)

//Asynchronous readFile
fs.readFile('/path/demo1.txt', function(err, data) {
  if (err) throw err;
  console.log(data);
});

fs.readFileSync(filename, [options])

//Synchronous readFile
var data = fs.readFileSync('/path/demo1.txt');
console.log(data);

writeFile : 写入文件

replacing the file if it already exists. data can be a string or a buffer.

fs.writeFile(filename, data, [options], callback)

//Asynchronous writeFile
fs.writeFile('/path/demo1.txt', 'hello yofine', function(err) {
  if (err) throw err;
  console.log('saved');
});

fs.writeFileSync(filename, data, [options])

//Synchronous writeFile
fs.writeFileSync('/path/demo1.txt', 'hello yofine');

appendFile : 附加写入文件

Asynchronously append data to a file, creating the file if it not yet exists. data can be a string or a buffer.

fs.appendFile(filename, data, [options], callback)

//Asynchronous appendFile
fs.appendFile('/path/demo1.txt', 'yofine', function(err) {
  if (err) throw err;
  console.log('Complete');
});

fs.appendFileSync(filename, data, [options])

//Synchronous appendFile
fs.appendFileSync('/path/demo1.txt', 'yofine');
console.log('Complete');

watchFile : 监视文件

fs.watchFile(filename, [options], listener)

fs.watchFile('/path/demo1.txt', function(curr, prev) {
  console.log('the current mtime is: ' + curr.mtime);
  console.log('the previous mtime was: ' + prev.mtime);
})

unwatchFile : 终止监视文件

fs.unwatchFile(filename, [listener])

fs.unwatchFile('/path/demo1.txt')

watch : 监视

Watch for changes on filename, where filename is either a file or a directory. The returned object is a fs.FSWatcher.

fs.watch(filename, [options], [listener])

fs.watch('/path/demo1.txt', function(event, filename) {
  console.log(event);
  console.log(filename);
});

exists : 检查是否存在

fs.exists(path, callback)

//Asynchronous exists
fs.exists('/path/demo1.txt', function(exists) {
  console.log(exists ? 'exists' : 'not exists');
})

fs.existsSync(path)

//Synchronous exists
var exists = fs.existsSync('/path/demo1.txt');
console.log(exists ? 'exists' : 'not exists');

createReadStream : 创建可读流

fs.createReadStream(path, [options])

options is an object with the following defaults:

{ flags: 'r',
  encoding: null,
  fd: null,
  mode: 0666,
  autoClose: true
}
fs.createReadStream('/path/demo1.txt', options);
var http = require('http');
var fs = requ{ flags: 'r',
  encoding: null,
  fd: null,
  mode: 0666,
  autoClose: true
}ire('fs');

http.createServer(function(req, res) {

  var filename = __dirname+req.url;

  var readStream = fs.createReadStream(filename);

  readStream.on('open', function () {
    readStream.pipe(res);
  });

  readStream.on('error', function(err) {
    res.end(err);
  });
}).listen(8080);

createWriteStream : 创建可读流

fs.createWriteStream(path, [options])

options is an object with the following defaults:

{ flags: 'w',
  encoding: null,
  mode: 0666 }
fs.createWriteStream('/path/demo1.txt', options)
var http = require('http');
var fs = require('fs');

http.createServer(function(req, res) {
  var writeStream = fs.createWriteStream('./output');

  req.pipe(writeStream);

  req.on('end', function () {
    res.writeHead(200, {"content-type":"text/html"});
    res.end('

<form method="POST"><input name="test" /><input type="submit"></form>

');
  });

  writeStream.on('error', function (err) {
    console.log(err);
  });
}).listen(8080);
    原文作者:Yofine
    原文地址: https://segmentfault.com/a/1190000000419497
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞