这个场景叫做fs。近是必要的

时间:2022-07-11 20:25:52

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:

我找不到关于fs的更多信息。在nodejs API中进行详细解释。我想知道这个情形叫什么fs。近是必要的。例如:

var fs =  require('fs');
fs.writeFile("/home/a.tex","abc"); or like fs.appendFile("/home/a.tex","close")
fs.close(); //is it necessary?

Are there any effects if i don't call fs.close?

如果我不叫f .close会有什么影响吗?

Any help is appreciated.

任何帮助都是感激。

1 个解决方案

#1


53  

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.

你不需要使用fs。fs后关闭。readFile,fs。writeFile或fs。appendFile,因为它们不返回fd(文件描述符)。打开文件,对其进行操作,然后为您关闭它。

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.

流由fs返回。createReadStream和fs。createWriteStream在流结束后关闭,但可能会提前关闭。如果您暂停了一个流,那么您必须在流上调用close来关闭fd或恢复流,并在发出所有数据后让它结束。

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.

但如果你叫它fs。打开或其他任何一个给出fd的,你最终必须是fs。关闭给定的fd。

#1


53  

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.

你不需要使用fs。fs后关闭。readFile,fs。writeFile或fs。appendFile,因为它们不返回fd(文件描述符)。打开文件,对其进行操作,然后为您关闭它。

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.

流由fs返回。createReadStream和fs。createWriteStream在流结束后关闭,但可能会提前关闭。如果您暂停了一个流,那么您必须在流上调用close来关闭fd或恢复流,并在发出所有数据后让它结束。

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.

但如果你叫它fs。打开或其他任何一个给出fd的,你最终必须是fs。关闭给定的fd。