Reputation: 6206
I've found various answers to this on Stack Overflow and elsewhere.
Should I do this:
let data = fs.readFileSync(FILE_NAME, "utf8");
Or this:
let data = fs.readFileSync(FILE_NAME, {encoding: "utf8"});
?
Upvotes: 9
Views: 26927
Reputation: 282
I don't think you need to mention encoding explicitly,since it is an optional parameter
var fs = require("fs");
/***
* implementation of readFileSync
*/
var data = fs.readFileSync('input.txt');
console.log(data.toString());
console.log("Program Ended");
/***
* implementation of readFile
*/
fs.readFile('input.txt', function (err, data) {
if (err) return console.error(err);
console.log(data.toString());
});
console.log("Program Ended");
this worked even without providing "encoding" argument
Upvotes: -1
Reputation: 79704
From the documentation, both are valid:
fs.readFileSync(path[, options])
- options <Object> | <string>
- encoding <string> | <null> Default: null
- flag <string> See support of file system flags. Default: 'r'.
The second argument may be either an options object, or an encoding name.
Upvotes: 14