node.js按行读取文件

let fs = require('fs');
let readline = require('readline');

function readFileToArr(fReadName, callback) {
    let fRead = fs.createReadStream(fReadName);
    let objReadline = readline.createInterface({
        input: fRead
    });
    let arr = new Array();
    objReadline.on('line', line => {
        arr.push(line);
    });
    objReadline.on('close', () => {
        callback(arr);
    });
}

readFileToArr('a.txt', arr => console.log(arr));

你可能感兴趣的:(node.js按行读取文件)