文件读取和流式传输有什么区别? [英] what is difference between file reading and streaming?

查看:258
本文介绍了文件读取和流式传输有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些书中读到使用流比在 node.js 中一次读取整个文件更好,我理解这个想法..但我想知道不是使用流读取文件,我已经习惯了 Java 和 C++,当我想读取文件时,我使用流.那么这里有什么区别??还有 fs.createReadStream();fs.readFile(); 之间的区别是什么两者都是异步的,对吧!!

I read in some book that using streaming is better than reading a whole file at a time in node.js, I understand the idea .. but I wonder isn't file reading using streams, I'm used to this from Java and C++, when I want to read a file I use streams .. So what's the difference here ?? also what is the difference between fs.createReadStream(<somefile>); and fs.readFile(<somefile>); both are asynchronous, right !!

推荐答案

首先是 fileread 是全缓冲方法.流是部分缓冲的方法.

First thing is fileread is fully buffered method. and streaming is partial buffered method.

现在是什么意思?

完全缓冲的函数调用,如 readFileSync() 和 readFile() 暴露数据作为一个大块.也就是说,执行读取,然后以同步或异步方式返回完整的数据集.使用这些全缓冲方法,我们必须等到所有数据都被读取,并且 Node 内部将需要分配足够的内存来将所有数据存储在内存中.这可能会出现问题 - 想象一个从磁盘读取 1 GB 文件的应用程序.如果仅使用完全缓冲访问,我们将需要使用 1 GB 内存来存储文件的全部内容以供读取 - 因为 readFile 和 readFileSync 都返回一个包含所有数据的字符串.

Fully buffered function calls like readFileSync() and readFile() expose the data as one big blob. That is, reading is performed and then the full set of data is returned either in synchronous or asynchronous fashion. With these fully buffered methods, we have to wait until all of the data is read, and internally Node will need to allocate enough memory to store all of the data in memory. This can be problematic - imagine an application that reads a 1 GB file from disk. With only fully buffered access we would need to use 1 GB of memory to store the whole content of the file for reading - since both readFile and readFileSync return a string containing all of the data.

部分缓冲的访问方法不同.它们不将数据输入视为离散事件,而是将其视为在读取或写入数据时发生的一系列事件.它们允许我们在从磁盘/网络/其他 I/O 读取数据时访问数据.

Partially buffered access methods are different. They do not treat data input as a discrete event, but rather as a series of events which occur as the data is being read or written. They allow us to access data as it is being read from disk/network/other I/O.

流返回数据的较小部分(使用缓冲区),并在新数据可用于处理时触发回调.

Streams return smaller parts of the data (using a Buffer), and trigger a callback when new data is available for processing.

流是事件发射器.例如,如果我们的 1 GB 文件需要以某种方式处理一次,我们可以使用流并在读取数据后立即对其进行处理.这很有用,因为我们不需要将内存中的所有数据保存在某个缓冲区中:处理后,对于此类应用程序,我们不再需要将数据保存在内存中.

Streams are EventEmitters. If our 1 GB file would, for example, need to be processed in some way once, we could use a stream and process the data as soon as it is read. This is useful, since we do not need to hold all of the data in memory in some buffer: after processing, we no longer need to keep the data in memory for this kind of application.

Node 流接口由两部分组成:可读流和可写流.一些流既可读又可写.

The Node stream interface consists of two parts: Readable streams and Writable streams. Some streams are both readable and writable.

这篇关于文件读取和流式传输有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆