资讯专栏INFORMATION COLUMN

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

RichardXG / 1330人阅读

摘要:同步写入读取读取文件写入文件附加写入文件监视文件终止监视文件监视检查是否存在创建可读流创建可读流

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("

"); }); writeStream.on("error", function (err) { console.log(err); }); }).listen(8080);

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/78010.html

相关文章

  • 撸个插件给你word-to-html

    摘要:最近遇到一个需求,需要将非常多内容的文字表格文档展示出来,这个需求出现在端就用插件好了或者直接下载文件如果需求是在移动端呢怎么办转成吧。。。下面是判断是否是非浏览器方法载入字符串对象解析文本并返回一个对象。 showImg(https://segmentfault.com/img/remote/1460000019821354?w=5000&h=2952); 最近遇到一个需求,需要将非...

    zhjx922 评论0 收藏0
  • 撸个插件给你word-to-html

    摘要:最近遇到一个需求,需要将非常多内容的文字表格文档展示出来,这个需求出现在端就用插件好了或者直接下载文件如果需求是在移动端呢怎么办转成吧。。。下面是判断是否是非浏览器方法载入字符串对象解析文本并返回一个对象。 showImg(https://segmentfault.com/img/remote/1460000019821354?w=5000&h=2952); 最近遇到一个需求,需要将非...

    CntChen 评论0 收藏0
  • 自己撸个简单的ps切图脚本(未完待续…)

    摘要:刚做完的一个项目里,为了切图方便,接触了下的脚本功能。这里解释一下,也就是本文所讨论的脚本,并不只有可以用,都是可以用的,不过需要调用各自不同的。一些脚本的线上参考资料常用部分汉化版常数表初识脚本留坑待续 刚做完的一个H5项目里,为了切图方便,接触了下Photoshop的脚本功能。从找资料、写脚本到实际能用全套跑了一圈下来发现,嗯,果然是挺难用的[捂脸]。不过虽然缺点满满,但PS这个平...

    draveness 评论0 收藏0
  • 撸个查询物流的小程序,欢迎体验

    摘要:微信搜索小程序查一查物流,或者扫一扫下图,欢迎来回复分享哦。小程序用框架开发的,方便快捷,写法类似,支持相关操作,已可以引入包,不过在微信开发者工具有以下注意事项。对应关闭转选项,关闭。对应关闭上传代码时样式自动补全选项,关闭。 微信搜索小程序 查一查物流,或者扫一扫下图,欢迎来回复分享哦。 showImg(https://segmentfault.com/img/bVbiR2p?w=...

    张巨伟 评论0 收藏0
  • 撸个查询物流的小程序,欢迎体验

    摘要:微信搜索小程序查一查物流,或者扫一扫下图,欢迎来回复分享哦。小程序用框架开发的,方便快捷,写法类似,支持相关操作,已可以引入包,不过在微信开发者工具有以下注意事项。对应关闭转选项,关闭。对应关闭上传代码时样式自动补全选项,关闭。 微信搜索小程序 查一查物流,或者扫一扫下图,欢迎来回复分享哦。 showImg(https://segmentfault.com/img/bVbiR2p?w=...

    JeOam 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<