I am trying to read the contents of a properties file in node. this is my call:
fs.readFile("server/config.properties", {encoding: 'utf8'}, function(err, data ) { console.log( data ); });
The console prints a buffer:
<Buffer 74 69 74 69 20 3d 20 74 6f 74 6f 0a 74 61 74 61 20 3d 20 74 75 74 75>
when I replace the code with this:
fs.readFile("server/config.properties", function(err, data ) { console.log( data.toString('utf8') ); });
it works fine. But the node documentation says the String is converted to utf8 if the encoding is passed in the options
the output of node --version is v0.10.2
What am I missing here?
thank you for your support
readFile() Method. Parameters: The method accept three parameters as mentioned above and described below: filename: It holds the name of the file to read or the entire path if stored at other location. encoding: It holds the encoding of file.
readFile() method is used to read the file. This method read the entire file into buffer. To load the fs module, we use require() method. It Asynchronously reads the entire contents of a file. Syntax: fsPromises.readFile( path, options )
fs. readFile() is an asynchronous and a very easy to use method for reading files of any type.
var content; fs. readFile('./Index. html', function read(err, data) { if (err) { throw err; } content = data; }); console.
Depending on the version of Node you're running, the argument may be just the encoding
:
fs.readFile("server/config.properties", 'utf8', function(err, data ) { console.log( data ); });
The 2nd argument changed to options
with v0.10:
- FS
readFile()
,writeFile()
,appendFile()
and their Sync counterparts now take anoptions
object (but the old API, anencoding
string, is still supported)
For former documentation:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With