C# 使用流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1404303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:08:34  来源:igfitidea点击:

C# using streams

c#stream

提问by Martijn

Streams are kind of mysterious to me. I don't know when to use which stream and how to use them. Can someone explain to me how streams are used?

Streams 对我来说有点神秘。我不知道何时使用哪个流以及如何使用它们。有人可以向我解释如何使用流吗?

If I understand correctly, there are three stream types:

如果我理解正确,则有三种流类型:

  • stream
  • read stream
  • write stream
  • stream
  • read stream
  • write stream

Is this correct? And, for example, what is the difference between a Memorystreamand a FileStream?

这样对吗?并且,例如, aMemorystream和 a之间有什么区别FileStream

采纳答案by Arsen Mkrtchyan

A streamis an object used to transfer data. There is a generic stream class System.IO.Stream, from which all other stream classes in .NET are derived. The Streamclass deals with bytes.

是用于传输数据的对象。有一个通用流类System.IO.Stream,.NET 中的所有其他流类都从该类派生而来。在Stream类的字节交易。

The concrete stream classes are used to deal with other types of data than bytes. For example:

具体的流类用于处理字节以外的其他类型的数据。例如:

  • The FileStreamclass is used when the outside source is a file
  • MemoryStreamis used to store data in memory
  • System.Net.Sockets.NetworkStreamhandles network data
  • FileStream当外部源是一个文件类用于
  • MemoryStream用于在内存中存储数据
  • System.Net.Sockets.NetworkStream处理网络数据

Reader/writer streams such as StreamReaderand StreamWriterare not streams - they are not derived from System.IO.Stream, they are designed to help to write and read data from and to stream!

读取器/写入器流,例如StreamReaderStreamWriter不是流 - 它们不是从 派生的System.IO.Stream,它们旨在帮助从流中写入和读取数据!

回答by Robban

I would start by reading up on streams on MSDN: http://msdn.microsoft.com/en-us/library/system.io.stream.aspx

我首先阅读 MSDN 上的流:http: //msdn.microsoft.com/en-us/library/system.io.stream.aspx

Memorystream and FileStream are streams used to work with raw memory and Files respectively...

Memorystream 和 FileStream 是分别用于处理原始内存和文件的流......

回答by AnthonyWJones

There is only one basic type of Stream. However in various circumstances some members will throw an exception when called because in that context the operation was not available.

只有一种基本类型的Stream. 但是,在各种情况下,某些成员在调用时会抛出异常,因为在该上下文中该操作不可用。

For example a MemoryStreamis simply a way to moves bytes into and out of a chunk of memory. Hence you can call Read and Write on it.

例如, aMemoryStream只是一种将字节移入和移出内存块的方法。因此,您可以在其上调用 Read 和 Write。

On the other hand a FileStreamallows you to read or write (or both) from/to a file. Whether you can actually Read or Write depends on how the file was opened. You can't Write to a file if you only opened it for Read access.

另一方面, aFileStream允许您从/向文件读取或写入(或两者)。您是否可以真正读取或写入取决于文件的打开方式。如果您只为读取访问权限打开文件,则无法写入文件。

回答by Mattias S

I wouldn't call those different kind of streams. The Stream class have CanRead and CanWrite properties that tell you if the particular stream can be read from and written to.

我不会称这些不同类型的流。Stream 类具有 CanRead 和 CanWrite 属性,它们告诉您是否可以读取和写入特定流。

The major difference between different stream classes (such as MemoryStream vs FileStream) is the backing store - where the data is read from or where it's written to. It's kind of obvious from the name. A MemoryStream stores the data in memory only, a FileStream is backed by a file on disk, a NetworkStream reads data from the network and so on.

不同流类(例如 MemoryStream 与 FileStream)之间的主要区别在于后备存储 - 数据从哪里读取或写入哪里。从名字就可以看出来。MemoryStream 仅将数据存储在内存中,FileStream 由磁盘上的文件支持,NetworkStream 从网络读取数据等等。

回答by meatvest

Streams are good for dealing with large amounts of data. When it's impractical to load all the data into memory at the same time, you can open it as a stream and work with small chunks of it.

流非常适合处理大量数据。当将所有数据同时加载到内存中是不切实际的时,您可以将其作为流打开并使用它的小块。

回答by Tim Williams

To expand a little on other answers here, and help explain a lot of the example code you'll see dotted about, most of the time you don't read and write to a stream directly. Streams are a low-level means to transfer data.

在这里对其他答案进行一些扩展,并帮助解释您将看到的许多示例代码,大多数情况下您不会直接读取和写入流。流是传输数据的低级手段。

You'll notice that the functions for reading and writing are all byte orientated, e.g. WriteByte(). There are no functions for dealing with integers, strings etc. This makes the stream very general-purpose, but less simple to work with if, say, you just want to transfer text.

您会注意到用于读取和写入的函数都是面向字节的,例如 WriteByte()。没有处理整数、字符串等的函数。这使得流非常通用,但如果您只想传输文本,则使用起来就不那么简单了。

However, .NET provides classes that convert between native types and the low-level stream interface, and transfers the data to or from the stream for you. Some notable such classes are:

但是,.NET 提供了在本机类型和低级流接口之间进行转换的类,并为您将数据传入或传出流。一些值得注意的此类是:

StreamWriter // Badly named. Should be TextWriter.
StreamReader // Badly named. Should be TextReader.
BinaryWriter
BinaryReader

To use these, first you acquire your stream, then you create one of the above classes and associate it with the stream. E.g.

要使用这些,首先获取流,然后创建上述类之一并将其与流相关联。例如

MemoryStream memoryStream = new MemoryStream();
StreamWriter myStreamWriter = new StreamWriter(memoryStream);

StreamReader and StreamWriter convert between native types and their string representations then transfer the strings to and from the stream as bytes. So

StreamReader 和 StreamWriter 在本机类型和它们的字符串表示之间进行转换,然后将字符串作为字节传入和传出流。所以

myStreamWriter.Write(123);

will write "123" (three characters '1', '2' then '3') to the stream. If you're dealing with text files (e.g. html), StreamReader and StreamWriter are the classes you would use.

将“123”(三个字符“1”、“2”和“3”)写入流。如果您正在处理文本文件(例如 html),StreamReader 和 StreamWriter 是您将使用的类。

Whereas

然而

myBinaryWriter.Write(123);

will write four bytes representing the 32-bit integer value 123 (0x7B, 0x00, 0x00, 0x00). If you're dealing with binary files or network protocols BinaryReader and BinaryWriter are what you might use. (If you're exchanging data with networks or other systems, you need to be mindful of endianness, but that's another post.)

将写入代表 32 位整数值 123(0x7B、0x00、0x00、0x00)的四个字节。如果您正在处理二进制文件或网络协议 BinaryReader 和 BinaryWriter 是您可能使用的。(如果您要与网络或其他系统交换数据,则需要注意字节顺序,但那是另一篇文章。)

回答by Anwar Husain

Stream is just an abstraction (or a wrapper) over a physicalstream of bytes. This physicalstream is called the base stream. So there is always a base stream over which a stream wrapper is created and thus the wrapper is named after the base stream type ie FileStream, MemoryStreametc.

Stream 只是physical对字节流的抽象(或包装器)。此physical流称为base stream. 所以总是有在其上流封包被创建并基本流类型之后即由此所述包装被命名为基本流FileStreamMemoryStream

The advantage of the stream wrapper is that you get a unified api to interact with streams of any underlying type usb, fileetc.

流包装器的优点是您可以获得一个统一的 API 来与任何底层类型的流usb, file等进行交互。

Why would you treat data as stream- because data chunks are loaded on-demand, we can inspect/process the data as chunks rather than loading the entire data into memory. This is how most of the programs deal with big files, for eg encrypting an OS image file.

为什么要将数据视为流- 因为数据块是按需加载的,我们可以将数据作为块检查/处理,而不是将整个数据加载到内存中。这是大多数程序处理大文件的方式,例如加密操作系统映像文件。