对node.js文件系统感到困惑 [英] Confused about node.js file system
问题描述
我通过两步在nodejs中使用了写文件:
1.首先判断文件是否存在,使用fs.exists
功能;
2.然后使用fs.writeFile
直接写入文件;
但是现在我注意到还有更多用于写入文件的功能,例如fs.open
或fs.close
,我在写入时应该使用这些功能来打开或关闭文件吗?
此外,我注意到还有fs.createReadStream
和fs.createWriteStream
函数,它们与fs.writeFile
和fs.readFile
有什么区别?
在这里,我将解释这些差异:
低级别:
fs.open 和 open(2) BSD系统调用.因为有了文件描述符,所以可以将它们与 fs.read 一起使用或 fs.write .
请注意,所有这些都是异步的,并且也有同步版本: fs.openSync , fs.closeSync , fs .writeSync ,您将不会在其中使用回调.异步版本和异步版本之间的区别在于,fs.openSync
仅在打开文件的操作完成后才会返回,而fs.open
会立即返回并且您将在回调中使用文件描述符.
这些低级功能可以完全控制您,但将意味着更多的编码.
中级:
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
.
这篇关于对node.js文件系统感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!