Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the scenario call fs.close is necessary

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.

like image 730
L.T Avatar asked Jan 17 '14 02:01

L.T


People also ask

What does require fs mean?

The Node.js file system module allows you to work with the file system on your computer. To include the File System module, use the require() method: var fs = require('fs'); Common use for the File System module: Read files.

Which method of fs is used to close a file?

close() Method. The fs. close() method is used to asynchronously close the given file descriptor thereby clearing the file that is associated with it. This will allow the file descriptor to be reused for other files.

What is fs module responsible for?

The fs module is responsible for all the asynchronous or synchronous file I/O operations. Let's see some of the common I/O operation examples using fs module.


1 Answers

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.

like image 90
Dan D. Avatar answered Sep 22 '22 02:09

Dan D.