Skip to content

Latest commit

 

History

History
16 lines (13 loc) · 458 Bytes

section4.12.md

File metadata and controls

16 lines (13 loc) · 458 Bytes

Section 4.12: Changing contents of a text file

Example. It will be replacing the word email to a name in a text file text.txt with simple RegExp replace('Hello, 'Hi')

let fs = require('fs');

fs.readFile('text.txt', 'utf-8', function(err, data) {
  if (err) throw err;
  let newValue = data.replace('Hello', 'Hi');
  fs.writeFile('output.txt', newValue, 'utf-8', function(err, data) {
    if (err) throw err;
    console.log('Done!');
  })
});