内存流和文件流之间的区别

编程入门 行业动态 更新时间:2024-10-25 04:19:58
本文介绍了内存流和文件流之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在序列化期间,我们可以使用内存流或文件流.

During the serialization we can use either memory stream or file stream.

这两者之间的基本区别是什么?内存流是什么意思?

What is the basic difference between these two? What does memory stream mean?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization.Formatters.Binary; namespace Serilization { class Program { static void Main(string[] args) { MemoryStream aStream = new MemoryStream(); BinaryFormatter aBinaryFormat = new BinaryFormatter(); aBinaryFormat.Serialize(aStream, person); aStream.Close(); } } }

推荐答案

流是字节的表示形式.这两个类都是从Stream类派生的,该类从定义上是抽象的.

Stream is a representation of bytes. Both these classes derive from the Stream class which is abstract by definition.

顾名思义,FileStream读写文件,而MemoryStream读写内存.因此,它与流的存储位置有关.

As the name suggests, a FileStream reads and writes to a file whereas a MemoryStream reads and writes to the memory. So it relates to where the stream is stored.

现在,这取决于您打算如何同时使用这两种方式.例如:假设您要从数据库中读取二进制数据,则需要使用MemoryStream.但是,如果要读取系统上的文件,则可以使用FileStream.

Now it depends how you plan to use both of these. For eg: Let us assume you want to read binary data from the database, you would go in for a MemoryStream. However if you want to read a file on your system, you would go in for a FileStream.

MemoryStream的一个快速优势是无需在应用程序中创建临时缓冲区和文件.

One quick advantage of a MemoryStream is that there is not need to create temporary buffers and files in an application.

更多推荐

内存流和文件流之间的区别

本文发布于:2023-11-08 15:47:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569793.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   内存   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!