encoding is ignored in fs.readFile

后端 未结 2 594
日久生厌
日久生厌 2021-02-07 01:47

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, dat         


        
相关标签:
2条回答
  • 2021-02-07 02:12

    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 an options object (but the old API, an encoding string, is still supported)

    For former documentation:

    • v0.8.22
    • v0.6.21
    0 讨论(0)
  • 2021-02-07 02:13

    You should change {encoding: 'utf8'} to {encoding: 'utf-8'}, for example:

    fs.readFile("server/config.properties", {encoding: 'utf-8'}, function(err, data ) {
    console.log( data );
    });
    
    0 讨论(0)
提交回复
热议问题