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
?
Node.js as a File Server 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');
Elixir is a well-known alternative to Node. js as it is a dynamic, functional language for creating maintainable and scalable applications. It is open-source and highly compatible with Mac, Windows, and Linux.
js FS Reading File. Every method in fs module has synchronous and asynchronous forms. Asynchronous methods take a last parameter as completion function callback. Asynchronous method is preferred over synchronous method because it never blocks the program execution where as the synchronous method blocks.
NodeJS is one of the most popular server-side programming frameworks running on the JavaScript V8 engine, which uses a single-threaded non-blocking I/O model. We can access the file system in NodeJS using some inbuilt modules.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With