内存流和文件流之间的区别 [英] difference between memory stream and filestream

查看:594
本文介绍了内存流和文件流之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于内存流和文件流之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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