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

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

问题描述

我在一些书中读到,使用流式传输比在 node.js 中一次读取整个文件更好,我理解这个想法..但我不知道是不是使用流来读取文件,我习惯于使用Java和C ++,当我想读取文件时我会使用流..那么有什么区别?
还有什么区别 fs.createReadStream(< somefile>); fs.readFile(< somefile>);
都是异步的,对吧!!

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()将
的数据公开为一个大blob。也就是说,执行读取,然后以同步或异步方式返回整组数据。
使用这些完全缓冲的方法,我们必须等到读取所有数据,并且内部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读取数据时访问数据。部分缓冲的访问方法是不同的。它们不将数据输入视为离散事件,而是将一系列事件视为正在读取或写入数据时发生的事件。它们允许我们在从磁盘/网络/其他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.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返回较小的数据部分(使用Buffer),并且当新数据可供处理时触发回调。

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

Streams是EventEmitters。例如,如果我们的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.

节点流接口由两部分组成:可读流和可写流。有些流是可读写的。

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

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

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