W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
可讀流的最重要的事件是“readable”。
當(dāng)存在要從流讀取的新數(shù)據(jù)時(shí)引發(fā)可讀事件。
我們可以調(diào)用流上的讀取函數(shù)從流中讀取數(shù)據(jù)。
如果這是流的結(jié)尾,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ù)導(dǎo)入process.stdin。
$ echo "foo bar bas" | node basic.js Got: "foo bar bas" Read complete!
要寫入流,請(qǐng)調(diào)用 write
以寫入一些數(shù)據(jù)。
當(dāng)你完成寫入,調(diào)用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號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: