A solution is proposed here: How to append to certain line of file?
I copy the solution here for reference
var fs = require('fs');
var xmlFile;
fs.readFile('someFile.xml', function (err, data) {
if (err) throw err;
xmlFile = data;
var newXmlFile = xmlFile.replace('</xml>', '') + 'Content' + '</xml>';
fs.writeFile('someFile.xml', newXmlFile, function (err) {
if (err) throw err;
console.log('Done!');
});
});
However the solution above requires string matching of the '</xml>' string. If we know that the last line of the file will always be '</xml>' is there any way to speed up the code by eliminating the need for string comparison? Is there an another more efficient way to do this task?