J
J
Jumandjilos2018-04-24 17:40:01
Node.js
Jumandjilos, 2018-04-24 17:40:01

How is readFIle and writeFile different from createReadStream and createWriteStream?

The question in the title is when to use streams and when to just write?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Arkhipov, 2018-04-24
@arvitaly

readFile reads the entire file and passes the content to the callback function. Suitable for small files, or for file types that do not support streaming reading (for example, you need to guarantee the integrity of all content). Plus, since all the content is stored in a variable, the entire contents of the file will be in memory.
createReadStream creates a FileStream object and makes it possible to read a file in parts. Accordingly, only the current piece will be in memory. For example, you can convert XML or JSON like this. Or send a file over the network.
With the record, everything is by analogy.
PS Both JSON and XML support streaming, but the file may be inconsistent, for example, corrupted at the end.
Upd. Answer corrected by @Smlnc and Aleksey Ten comment

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question