如何在Node.js中逐字节读取二进制文件 [英] How to read binary files byte by byte in Node.js
本文介绍了如何在Node.js中逐字节读取二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Node.js中读取二进制文件的一部分的最佳方法是什么?
What is the best way to read part of a binary file in Node.js?
我希望访问标题中的特定字节(更少)比前100个字节)或逐字节读取文件。
I am looking to either access specific bytes in the "header" (less than the first 100 bytes) or read the file byte by byte.
推荐答案
以下是的示例fs.read()
- 来自 fs.open()返回的文件描述符的前100个字节
:
var fs = require('fs');
fs.open('file.txt', 'r', function(status, fd) {
if (status) {
console.log(status.message);
return;
}
var buffer = Buffer.alloc(100);
fs.read(fd, buffer, 0, 100, 0, function(err, num) {
console.log(buffer.toString('utf8', 0, num));
});
});
这篇关于如何在Node.js中逐字节读取二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文