In my project, i am using fs.watchFile for listening to the modification of the text file.
Requirement Read only the last updated data
Note In text file data is always added, no deletion.
Sample code
fs.watchFile(config.filePath, function (curr, prev) {
fs.readFile(config.filePath, function (err, data) {
if (err) throw err;
console.log(data);
});
});
Above code reads whole text file when the file is modified.
Any suggestion will be greatful.
Working Code
fs.watchFile(config.filePath, function (curr, prev) {
var filestream = fs.createReadStream(config.filePath,{start:prev.size,end:curr.size,encoding:"utf-8");
filestream.on('data', function (data) {
console.log(data);
});
});
You can work with the stat object of the file. The curr and the prev object are both Stats objects and have an attribute called "size".
I assume you are always adding data to the beginning or the end of the file, otherwise there is no way in knowing where the data was added.
The difference between prev.size and curr.size tells you how many bytes were added. By give the readFile-Function an options object with a start and an end attribute.
For example: You always add to the end, then you can make such a call:
fs.readFile(config.filePath, {start: prev.size}, function ...);
If you add in the beginning:
fs.readFile(config.filePath, {start: 0, end: (curr.size-prev.size)}, function ...);
Hope that helps!