可讀。可讀屬性是Stream模塊的內置應用程序編程接口,用於檢查調用read.read()方法是否安全。
用法:
readable.readable
返回值:如果可讀,則返回true.read()方法被調用,否則返回false。
以下示例說明了Node.js中可讀屬性的使用:
範例1:
// Node.js program to demonstrate the
// readable.readable Property
// Include fs module
const fs = require('fs');
// Creating readable stream
const readable = fs.createReadStream("input.txt");
// Calling readable property
readable.readable;
輸出:
true
範例2:
// Node.js program to demonstrate the
// readable.readable Property
// Include fs module
const fs = require("fs");
// Constructing readable stream
const readable = fs.createReadStream("input.txt");
// Instructions for reading data
readable.on('readable', () => {
let chunk;
// Using while loop and calling
// read method
while (null !== (chunk = readable.read())) {
// Displaying the chunk
console.log(`read:${chunk}`);
}
});
// Shows that the program
// ended
console.log("Program ends!!");
// Calling readable property
readable.readable;
輸出:
Program ends!! true read:hello
參考: https://nodejs.org/api/stream.html#stream_readable_readable
相關用法
- node.js Stream writable.writableLength用法及代碼示例
- node.js Stream writable.destroyed用法及代碼示例
- node.js Stream writable.writableEnded用法及代碼示例
- node.js Stream readable.destroyed用法及代碼示例
- node.js Stream writable.writableCorked用法及代碼示例
- node.js Stream writable.writableHighWaterMark用法及代碼示例
- node.js Stream writable.writableFinished用法及代碼示例
- node.js Stream writable.writableObjectMode用法及代碼示例
- node.js Stream writable.writable用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | Stream readable.readable Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。