node.js - nodejs asynchronously reading continuously growing file -
i trying read text file continuously growing (adding new lines @ end) high rate, lets ~100 lines per seconds line size approximate 200 characters.
i tried following , working lagging minute , so.
var fs = require('fs'); var path = "d:\\testreadwrite.txt"; fs.watchfile(path, function() { console.log('file changed ...'); file = fs.readfilesync(path); console.log('file content @ : ' + new date() + ' \n' + file); }); i not need synchronous reading lagging > 1 minute high, , need entire file each time. need , read data , process line line, each new line coming. tried below code planning loop through , pass offset each iteration. code not working unknown reasons. please help.
var fs = require('fs'); var path = "d:\\work\\jai ho\\myapp\\public\\testreadwrite.txt"; fs.watch(path, function(event, filename) { if(filename){ fs.stat(path, function(error, stats) { fs.open(path, "r", function(error, fd) { var buffer = new buffer(stats.size); fs.read(fd, buffer, 0, buffer.length, null, function(error, bytesread, buffer) { var data = buffer.tostring("utf8"); console.log(data); }); }); }); } else{ console.log('filename not provided') } });
Comments
Post a Comment