hh54188
hh54188

Reputation: 15646

Confused about node.js file system

I used write file with nodejs in two steps:

1.First judge if the file is exist or not,use fs.exists function;

2.Then use fs.writeFile to write file directly;

But now I have notice there have more functions used for write file, like fs.open or fs.close, should I use these for open or close file while writing?

Besides, I noticed there have fs.createReadStream and fs.createWriteStream function , what's the differences between them and fs.writeFile and fs.readFile?

Upvotes: 10

Views: 5692

Answers (1)

beny23
beny23

Reputation: 35048

Here's how I would explain the differences:

Low-level:

fs.open and fs.close work on file descriptors. These are low-level functions and represent map calls to open(2) BSD system calls. As you'll have a file descriptor, you'd be using these with fs.read or fs.write.

Note, all these are asynchronous and there are synchronous versions as well: fs.openSync, fs.closeSync, fs.readSync, fs.writeSync, where you wouldn't use a callback. The difference between the asynchronous and synchronous versions is that fs.openSync would only return when the operation to open the file has completed, whereas fs.open returns straight away and you'd use the file descriptor in the callback.

These low-level functions give you full control, but will mean a lot more coding.

Mid level:

fs.createReadStream and fs.createWriteStream create stream objects which you can wire up to events. Examples for these events are 'data' (when a chunk of data has been read, but that chunk is only part of the file) or 'close'. Advantages of this are that you can read a file and process it as data comes in, i.e. you don't have to read the whole file, keep it in memory and then process it. This makes sense when dealing with large files as you can get better performance in processing bits in chunks rather than dealing with the whole file (e.g. a whole 1GB file in memory).

High level:

fs.readFile and fs.writeFile operate on the whole file. So you'd call fs.readFile, node would read in the whole file and then present you the whole data in your callback. The advantage of this is that you don't need to deal with differently sized chunks (like when using streams). When writing, node would write the whole file. The disadvantage of this approach is that when reading/writing, you'd have to have the whole file in memory. For example, if you are transforming a log file, you may only need lines of data, using streams you can do this without having to wait for the file to be read in completely before starting to write.

There are also, fs.readFileSync and fs.writeFileSync which would not use a callback, but wait for the read/write to finish before returning. The advantage of using this is that for a small file, you may not want to do anything before the file returns, but for big files it would mean that the CPU would idle away while waiting for the file I/O to finish.

Hope that makes sense and in answer to your question, when using fs.writeFile you don't need fs.open or fs.close.

Upvotes: 32

Related Questions