I am trying to read a text file which is continuously growing (adding new lines at end) with very high rate, lets say ~100 lines per seconds where line size is approximate 200 characters.
I tried following , which is working but lagging by a minute and 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 at : ' + new Date() + ' is \n' + file);
});
I really do not need synchronous reading but lagging > 1 minute is too high, and i do need entire file each time. All i need is , read the data and process line by line, for each new line coming. so i tried below code where i am planning to loop through and pass offset for each iteration. But this code is not working for some 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')
}
});