简体   繁体   English

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

[英]Node.js read stream not reading properly from file

I have a file that has one line with an id I need to use. 我有一个文件,其中有一行需要使用的ID。 In this case the id is on the first line of the file and it is this 9e598f1c-62d4-4b39-ac2d-ca8c5611c6b7 在这种情况下,id位于文件的第一行,它是此9e598f1c-62d4-4b39-ac2d-ca8c5611c6b7

But when I try to read from the file using a read stream with this code 但是当我尝试使用带有此代码的读取流从文件读取时

  var file_stream = fs.createReadStream(file_path);

  file_stream.on('readable', () => {
  console.log('readable:', file_stream.read());
  });

  file_stream.on('end', () => {
  console.log('end');
  });

I get this output 我得到这个输出

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

Which is definitely not what I had in the file. 这绝对不是我在文件中所拥有的。 What could be happening? 可能会发生什么?

It is the contents of your file, just in byte notation. 它是文件的内容,只是字节表示法。 Try this to get it back: 尝试以下操作将其取回:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM