1// basic read-write string from-to file with node.js
2var fs = require('fs');
3var opath = 'test.txt';
4var ostring = 'Hello!'
5fs.writeFileSync(opath, ostring, 'utf8');
6var istring = fs.readFileSync('test.txt').toString();
7console.log(istring);
8
9// with error handling and logging:
10fs.readFile('test.txt', 'utf8' , (err, data) => {
11 if (err) {
12 console.error(err)
13 return
14 }
15 console.log(data)
16 });
17
18// or fs.readFileSync() or use streams for large files
19// for better memory consumption and execution speed
20// https://nodejs.dev/learn/reading-files-with-nodejs