本文介绍了Node.js 可读文件流未获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个可读文件流,我可以从中读取单个字节.我正在使用下面的代码.
I'm attempting to create a Readable file stream that I can read individual bytes from. I'm using the code below.
var rs = fs.createReadStream(file).on('open', function() {
var buff = rs.read(8); //Read first 8 bytes
console.log(buff);
});
鉴于该文件是至少 8 个字节的现有文件,为什么我将空"作为输出?
Given that file is an existing file of at least 8 bytes, why am I getting 'null' as the output for this?
推荐答案
Event open
表示流已经初始化,并不表示可以从流中读取.您必须监听 readable
或 data
事件.
Event open
means that stream has been initialized, it does not mean you can read from the stream. You would have to listen for either readable
or data
events.
var rs = fs.createReadStream(file);
rs.once('readable', function() {
var buff = rs.read(8); //Read first 8 bytes only once
console.log(buff.toString());
});
这篇关于Node.js 可读文件流未获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!