Read a file one line at a time in node.js?

前端 未结 29 1085
深忆病人
深忆病人 2020-11-22 04:33

I am trying to read a large file one line at a time. I found a question on Quora that dealt with the subject but I\'m missing some connections to make the whole thing fit to

29条回答
  •  既然无缘
    2020-11-22 05:03

    function createLineReader(fileName){
        var EM = require("events").EventEmitter
        var ev = new EM()
        var stream = require("fs").createReadStream(fileName)
        var remainder = null;
        stream.on("data",function(data){
            if(remainder != null){//append newly received data chunk
                var tmp = new Buffer(remainder.length+data.length)
                remainder.copy(tmp)
                data.copy(tmp,remainder.length)
                data = tmp;
            }
            var start = 0;
            for(var i=0; i

提交回复
热议问题