国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

每天擼個API -- File System (4)

RichardXG / 1469人閱讀

摘要:同步寫入讀取讀取文件寫入文件附加寫入文件監視文件終止監視文件監視檢查是否存在創建可讀流創建可讀流

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);

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/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元查看
<