Node.js读取流无法从文件中正确读取

时间:2016-02-24 11:52:13

标签: node.js stream

我有一个文件,其中有一行我需要使用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

这绝对不是我在文件中的内容。可能会发生什么?

1 个答案:

答案 0 :(得分:3)

它是文件的内容,只是字节表示法。试试这个来取回它:

file_stream.on('readable', () => {
  var buf = file_stream.read()
  if (buf != null) {
    console.log('readable:', buf.toString());
  }
});