Sunday, February 22, 2015

How to insert a line into a file in NodeJS

How to insert a text in to a specific line in a text file using NodeJS.

function insertLine(file, lineNumber, insertText) {   
  var array = fs.readFileSync(file).toString().split("\n");
array.splice((lineNumber - 1), 0, insertText); fs.writeFileSync(file, array.join('\n'), 'utf8');
}

No comments:

Post a Comment