nodejs同步逐行读取大文件?
我有一个大文件(utf8)。我知道 fs.createReadStream 可以创建流来读取大文件,但不同步。所以我尝试使用fs.readSync
,但读取文本像“迈�”
一样被破坏。
var fs = require('fs');
var util = require('util');
var textPath = __dirname + '/people-daily.txt';
var fd = fs.openSync(textPath, "r");
var text = fs.readSync(fd, 4, 0, "utf8");
console.log(util.inspect(text, true, null));
I have a large file (utf8). I know fs.createReadStream
can create stream to read a large file, but not synchronized. So i try to use fs.readSync
, but read text is broken like "迈�"
.
var fs = require('fs');
var util = require('util');
var textPath = __dirname + '/people-daily.txt';
var fd = fs.openSync(textPath, "r");
var text = fs.readSync(fd, 4, 0, "utf8");
console.log(util.inspect(text, true, null));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于大文件,
readFileSync
可能不方便,因为它将整个文件加载到内存中。另一种不同的同步方法是迭代调用 readSync,一次读取少量数据,并在数据行到来时对其进行处理。以下代码实现了这种方法,并一次同步处理文件“test.txt”中的一行:For large files,
readFileSync
can be inconvenient, as it loads the whole file in memory. A different synchronous approach is to iteratively callreadSync
, reading small bits of data at a time, and processing the lines as they come. The following bit of code implements this approach and synchronously processes one line at a time from the file 'test.txt':使用 https://github.com/nacholibre/node-readlines
use https://github.com/nacholibre/node-readlines
使用 readFileSync:
顺便说一句,由于您使用的是节点,所以我建议使用异步函数。
Use readFileSync:
On a side note, since you are using node, I'd recommend using asynchronous functions.
我构建了一个更简单的版本 JB Kohn 的答案,它在缓冲区上使用 split() 。它适用于我尝试过的较大文件。
I built a simpler version JB Kohn's answer that uses split() on the buffer. It works on the larger files I tried.
两个潜在的问题,
two potential problems,
Node 11 为此添加了异步迭代器语法。请参阅相关答案长话短说
:
Node 11 added an async iterator syntax for this. See a related answer here
Long story short: