1. Reading file
const fs = require('fs');
fs.readFile("/tmp/test.txt", "utf8", (err, data) => {
console.log(data);
});
-
fs.readFile(- reads file and returns data in callback function, -
/tmp/test.txt- path to text file to read, -
utf8- encoding to read text in, -
console.log(data)- log date from file to console.
Open original or edit on Github.
2. Writing file
const fs = require('fs');
fs.writeFile('/tmp/test.txt', 'hi!', (err, data) => {
console.log(err);
});
-
require('fs')- library to work with file system, -
/tmp/test.txt- path of file to write, -
'hi!'- Data to write to file, -
console.log(err)- if err is not null, something wrong happened, -
fs.writeFile(- writes given data to specified file.
Top comments (0)