What the scenario call fs.close is necessary

前端 未结 1 1159
醉话见心
醉话见心 2020-12-01 04:35

I can\'t find more about fs.close explain in nodejs API. I want to know what the scenario call fs.close is necessary. for example:

var fs =  require(\'fs\');
fs.w         


        
相关标签:
1条回答
  • 2020-12-01 04:55

    You don't need to use fs.close after fs.readFile, fs.writeFile, or fs.appendFile as they don't return a fd (file descriptor). Those open the file, operate on it, and then close it for you.

    The streams returned by fs.createReadStream and fs.createWriteStream close after the stream ends but may be closed early. If you have paused a stream, you must call close on the stream to close the fd or resume the stream and let it end after emitting all its data.

    But if you call fs.open or any of the others that give a fd, you must eventually fs.close the fd that you are given.

    0 讨论(0)
提交回复
热议问题