roland
roland

Reputation: 7775

Nodejs: Writing a lot of files: createWriteStream or writeFile?

I have to write a lot of files and I'm wondering which method suits best. The data are text.

I'm a bit concerned on how I can close properly the operation. createWriteStream has a end() method or fs.close(), which closes the stream; writeFile has no such method if I well-understood, which may be inefficient after a long batch of write. All the examples of fs.writeFile never mention fs.close().

Upvotes: 3

Views: 3200

Answers (2)

hexacyanide
hexacyanide

Reputation: 91639

fs.close() is actually used to close a file descriptor optained from fs.open() or similar functions. What you might be looking at it is writeable.close(). fs.write() will fully write and finish a file in a single call.

For writing streams, you must close them when finished writing unless you use readable.pipe(). The main difference between fs.createReadStream() and fs.writeFile() is that one accepts a stream and one accepts a in-memory resource. Therefore if you have a large file, it is best to use a stream so only small chunks are loading into memory at one time.

Upvotes: 2

lapo
lapo

Reputation: 3224

writeFile doesn't need a close method because it opens/writes/closes the file all in a single call.

The main criteria do decide between the two is not the presence/need of an explicit close but rather: does the data fit easily in RAM? If it does, then writeFile is certainly simpler to use (and potentially a slightly bit faster); else, you better use streams and write data in chunks.

Upvotes: 3

Related Questions