🎃
[自分メモ用]nodejsで大量のテキストを読み込む
stream
だけでやれば、行の途中でchunkが生じる。
解決策:readline
のcreateInterface
を導入。
import { createReadStream } from 'fs';
import * as readlinePromises from 'node:readline/promises';
async function readFileByLine(fileName){
const rs = createReadStream(fileName, {
encoding: 'utf-8',
});
const rl = readlinePromises.createInterface({
input: rs,
crlfDelay: Infinity,
});
for await (const line of rl) {
// do something
}
}
Discussion