【问题标题】:Node.js append raw bytes to fileNode.js 将原始字节附加到文件
【发布时间】:2022-02-04 09:20:30
【问题描述】:

我想在原始数据文件中附加 4 个字节。

此代码将写入文本,但没有处理原始字节的指南

fs = require('fs');
fs.writeFile('helloworld.txt', 'Hello World!', function (err) {
  if (err) return console.log(err);
  console.log('Hello World > helloworld.txt');
});

【问题讨论】:

标签: node.js fs


【解决方案1】:

fs.writeFile() 接受 Buffer 以及字符串。

此示例将三个 0x01 字节附加到文件中:

const fs = require('fs');

fs.writeFile('helloworld.txt', Buffer.from([0x01, 0x01, 0x01]), { flag: "a" }, function (err) {
  if (err) return console.log(err);
  console.log('Hello World > helloworld.txt');
});

【讨论】:

    猜你喜欢
    • 2020-05-27
    • 2015-03-10
    • 2015-06-16
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多