node.js - nodejs以二进制模式创建readstream?

标签 node.js file stream

我可以使用 python 以二进制模式打开文件

open ('filename', 'rb') as f:
    for line in f:
        print line

如何在 Node 中执行此操作?

input = fs.createReadStream filePath, {flags: "rb"}

无论如何,这是错误的

最佳答案

If no encoding is specified, then the raw buffer is returned.

fs.readFile(file[, options], callback)

fs.readFile('filename', (err, buffer) => {
  if (err) throw err;

  console.log(`${buffer}: ${buffer.length} characters, ` +
    `${Buffer.byteLength(buffer, 'utf8')} bytes`);
});

The Buffer class is a global type for dealing with binary data directly. It can be constructed in a variety of ways.

关于node.js - nodejs以二进制模式创建readstream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34761465/

相关文章:

node.js - 使用 Mocha 读取生态系统变量(单元测试)

c - 使用重命名功能移动文件而不是使用读/写功能有什么缺点吗?

java - 如何从另一个 Flux 中排除 Flux 中的所有元素

c - 为什么文件数据不保存到结构中?

java - 重新序列化后,对象是否可能具有不同的字节大小?

c# - 在 C# 中使用 (IDisposable obj = new ...) 在流中写入代码块(例如 XML)

node.js - 无法发送空消息

node.js - 为什么 NPM 安装 bcrypt 失败

javascript - 没有 Node 服务器的 Angular2 应用程序不从 index.html 开始

Linux 文件所有权和权限