我有一个文件,其中一行包含我需要使用的 id。在这种情况下,id 位于文件的第一行,它是这样的 9e598f1c-62d4-4b39-ac2d-ca8c5611c6b7
但是当我尝试使用带有此代码的读取流从文件中读取时
var file_stream = fs.createReadStream(file_path);
file_stream.on('readable', () => {
console.log('readable:', file_stream.read());
});
file_stream.on('end', () => {
console.log('end');
});
我得到这个输出
readable: <Buffer 39 65 35 39 38 66 31 63 2d 36 32 64 34 2d 34 62 33 39 2d 61 63 32 64 2d 63 61 38 63 35 36 31 31 63 36 62 37>
readable: null
end
这绝对不是我在文件中的内容。可能会发生什么?
最佳答案
它是文件的内容,仅以字节表示法表示。尝试以下方法将其恢复:
file_stream.on('readable', () => {
var buf = file_stream.read()
if (buf != null) {
console.log('readable:', buf.toString());
}
});
关于Node.js 读取流无法从文件中正确读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35601446/