Node.js Stream readable.readableEncoding 属性
可读 Stream 中的readable.readableEncodin 属性用于获取声明的可读流的属性编码。此外,您可以使用 readable.setEncoding() 方法设置此属性。
句法:
readable.readableEncoding
返回值:返回程序中使用的编码。
下面的示例说明了 Node.js 中readable.readableEncoding 属性的使用:
示例 1:
// Node.js program to demonstrate the
// readable.readableEncoding Property
// Include fs module
const fs = require("fs");
// Constructing readable stream
const readable = fs.createReadStream("input.text");
// Setting the encoding
readable.setEncoding("base64");
// Instructions for reading data
readable.on('readable', () => {
let chunk;
// Using while loop and calling
// read method with parameter
while (null !== (chunk = readable.read())) {
// Displaying the chunk
console.log(`read: ${chunk}`);
}
});
// Calling readableEncoding property
readable.readableEncoding;
输出:
read: aGVs
read: bG8=
示例 2:
// Node.js program to demonstrate the
// readable.readableEncoding Property
// Include fs module
const fs = require("fs");
// Constructing readable stream
const readable = fs.createReadStream("input.text");
// Setting the encoding
readable.setEncoding("hex");
// Instructions for reading data
readable.on('readable', () => {
let chunk;
// Using while loop and calling
// read method with parameter
while (null !== (chunk = readable.read())) {
// Displaying the chunk
console.log(`read: ${chunk}`);
}
});
// Calling readableEncoding property
readable.readableEncoding;
输出:
read: 68656c6c6f
参考: https://nodejs.org/api/stream.html#stream_readable_readableencoding。