问题描述
我在 nodejs API 中找不到关于 fs.close 的更多解释.我想知道什么场景调用 fs.close 是必要的.例如:
var fs = require('fs');fs.writeFile("/home/a.tex","abc");或者像 fs.appendFile("/home/a.tex","close")fs.close();//有必要吗?不调用fs.close有什么影响吗?
感谢任何帮助.
fs.readFile
、fs 之后不需要使用
或 fs.close
.writeFilefs.appendFile
,因为它们不返回 fd
(文件描述符).那些打开文件,对其进行操作,然后为您关闭它.
fs.createReadStream
和 fs.createWriteStream
返回的流在流结束后关闭,但可能提前关闭.如果您暂停了一个流,您必须在该流上调用 close 来关闭 fd
或恢复该流并让它在发出所有数据后结束.
但是如果您调用 fs.open
或任何其他提供 fd
的其他函数,您最终必须 fs.close
>fd
你得到的.
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.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?
Any help is appreciated.
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.
这篇关于场景调用 fs.close 是必要的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!