W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
可讀流的最重要的事件是“readable”。
當存在要從流讀取的新數(shù)據(jù)時引發(fā)可讀事件。
我們可以調用流上的讀取函數(shù)從流中讀取數(shù)據(jù)。
如果這是流的結尾,read函數(shù)返回null。
將以下代碼保存為basic.js。
process.stdin.on("readable", function () {
var buf = process.stdin.read();
if (buf != null) {
console.log("Got:");
process.stdout.write(buf.toString());
} else {
console.log("Read complete!");
}
});
以下代碼顯示了如何從命令行將數(shù)據(jù)導入process.stdin。
$ echo "foo bar bas" | node basic.js Got: "foo bar bas" Read complete!
要寫入流,請調用 write
以寫入一些數(shù)據(jù)。
當你完成寫入,調用end。
你可以使用 end
成員函數(shù)寫入一些數(shù)據(jù)。
var fs = require("fs");
var ws = fs.createWriteStream("message.txt");
ws.write("this is a test");
ws.end("bas");
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: