美文网首页
node.js按行读取文件

node.js按行读取文件

作者: 择一人 | 来源:发表于2019-04-25 09:37 被阅读0次
    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按行读取文件

          本文链接:https://www.haomeiwen.com/subject/bxfagqtx.html