I used write file with nodejs in two steps:
我在两个步骤中使用了带有nodejs的写文件:
1.First judge if the file is exist or not,use fs.exists
function;
1.首先判断文件是否存在,使用fs.exists函数;
2.Then use fs.writeFile
to write file directly;
2.然后使用fs.writeFile直接写文件;
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?
但是现在我注意到有更多的函数用于写文件,比如fs.open或fs.close,我应该在写入时使用这些函数来打开还是关闭文件?
Besides, I noticed there have fs.createReadStream
and fs.createWriteStream
function , what's the differences between them and fs.writeFile
and fs.readFile
?
此外,我注意到有fs.createReadStream和fs.createWriteStream函数,它们与fs.writeFile和fs.readFile之间的区别是什么?
1 个解决方案
#1
29
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.
fs.open和fs.close处理文件描述符。这些是低级函数,表示打开(2)BSD系统调用的映射调用。因为你有一个文件描述符,你将使用fs.read或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.
注意,所有这些都是异步的,并且还有同步版本:fs.openSync,fs.closeSync,fs.readSync,fs.writeSync,您不会使用回调。异步和同步版本之间的区别在于,fs.openSync仅在打开文件的操作完成时返回,而fs.open会立即返回,并且您将在回调中使用文件描述符。
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).
fs.createReadStream和fs.createWriteStream创建可以连接到事件的流对象。这些事件的示例是“数据”(当读取了一大块数据,但该块只是文件的一部分时)或“关闭”。这样做的好处是,您可以读取文件并在数据进入时对其进行处理,即您不必读取整个文件,将其保存在内存中然后进行处理。这在处理大型文件时很有意义,因为您可以在处理块中的位时获得更好的性能,而不是处理整个文件(例如,内存中的整个1GB文件)。
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.
fs.readFile和fs.writeFile对整个文件进行操作。因此,您将调用fs.readFile,节点将读取整个文件,然后在回调中显示整个数据。这样做的好处是您不需要处理不同大小的块(比如使用流时)。写入时,node会写入整个文件。这种方法的缺点是,在读/写时,您必须将整个文件放在内存中。例如,如果要转换日志文件,则可能只需要数据行,使用流可以执行此操作,而无需等待文件在开始写入之前完全读取。
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.
还有fs.readFileSync和fs.writeFileSync,它们不会使用回调,而是在返回之前等待读/写完成。使用它的好处是,对于一个小文件,你可能不希望在文件返回之前做任何事情,但对于大文件,这意味着CPU在等待文件I / O完成时会空闲。
Hope that makes sense and in answer to your question, when using fs.writeFile
you don't need fs.open
or fs.close
.
希望有意义并回答你的问题,当使用fs.writeFile时,你不需要fs.open或fs.close。
#1
29
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.
fs.open和fs.close处理文件描述符。这些是低级函数,表示打开(2)BSD系统调用的映射调用。因为你有一个文件描述符,你将使用fs.read或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.
注意,所有这些都是异步的,并且还有同步版本:fs.openSync,fs.closeSync,fs.readSync,fs.writeSync,您不会使用回调。异步和同步版本之间的区别在于,fs.openSync仅在打开文件的操作完成时返回,而fs.open会立即返回,并且您将在回调中使用文件描述符。
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).
fs.createReadStream和fs.createWriteStream创建可以连接到事件的流对象。这些事件的示例是“数据”(当读取了一大块数据,但该块只是文件的一部分时)或“关闭”。这样做的好处是,您可以读取文件并在数据进入时对其进行处理,即您不必读取整个文件,将其保存在内存中然后进行处理。这在处理大型文件时很有意义,因为您可以在处理块中的位时获得更好的性能,而不是处理整个文件(例如,内存中的整个1GB文件)。
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.
fs.readFile和fs.writeFile对整个文件进行操作。因此,您将调用fs.readFile,节点将读取整个文件,然后在回调中显示整个数据。这样做的好处是您不需要处理不同大小的块(比如使用流时)。写入时,node会写入整个文件。这种方法的缺点是,在读/写时,您必须将整个文件放在内存中。例如,如果要转换日志文件,则可能只需要数据行,使用流可以执行此操作,而无需等待文件在开始写入之前完全读取。
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.
还有fs.readFileSync和fs.writeFileSync,它们不会使用回调,而是在返回之前等待读/写完成。使用它的好处是,对于一个小文件,你可能不希望在文件返回之前做任何事情,但对于大文件,这意味着CPU在等待文件I / O完成时会空闲。
Hope that makes sense and in answer to your question, when using fs.writeFile
you don't need fs.open
or fs.close
.
希望有意义并回答你的问题,当使用fs.writeFile时,你不需要fs.open或fs.close。