node.js 场景调用 fs.close 是必要的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21176733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:40:43  来源:igfitidea点击:

What the scenario call fs.close is necessary

node.jsfile-io

提问by L.T

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:

我在 nodejs API 中找不到关于 fs.close 的更多解释。我想知道什么场景调用 fs.close 是必要的。例如:

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?

如果我不调用 fs.close 有什么影响吗?

Any help is appreciated.

任何帮助表示赞赏。

回答by Dan D.

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

您不需要使用fs.closeafter fs.readFilefs.writeFilefs.appendFile因为它们不返回fd(文件描述符)。那些打开文件,对其进行操作,然后为您关闭它。

The streams returned by fs.createReadStreamand fs.createWriteStreamclose 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 fdor resume the stream and let it end after emitting all its data.

流返回fs.createReadStreamfs.createWriteStream在流结束后关闭,但可能会提前关闭。如果您已暂停流,则必须对流调用 close 以关闭fd或恢复流,并在发出所有数据后让它结束。

But if you call fs.openor any of the others that give a fd, you must eventually fs.closethe fdthat you are given.

但是如果你打电话fs.open或任何其他人给出了fd,你最终必须得到你fs.closefd那个。