【问题标题】:How to read byte array into FileStream如何将字节数组读入 FileStream
【发布时间】:2013-03-08 10:59:37
【问题描述】:

我有一个字节数组,我想将字节数组读入 FileStream。以下是我的代码示例:

string fileName = "test.txt";
byte[] file = File.ReadAllBytes(Server.MapPath("~/Files/" + fileName));
FileStream fs = new FileStream();
fs.ReadByte(file);
object obj = LoadFile<object>(fs);

public static T LoadFile<T>(FileStream fs)
{
    using (GZipStream gzip = new GZipStream(fs, CompressionMode.Decompress))
    {
        BinaryFormatter bf = new BinaryFormatter();
        return (T)bf.Deserialize(gzip);
    }
}

在上面的方法中,我使用 FileStream 读取字节数组,但不幸的是 fs.ReadByte 无法读取字节数组。任何帮助请关注如何将字节数组读入 FileStream 以用作方法“LoadFile”中的参数。请不要将文件直接读入 FileStream,因为这里的文件是从其他地方加载的,例如从数据库或其他来源。

【问题讨论】:

  • 为什么不使用 MemoryStream 而不是 FileStream?
  • MemoryStream 可以在 LoadFile 方法上用作 FileStream 吗?
  • 你的代码如何编译FileStream在构造函数中不接受0个参数

标签: c#


【解决方案1】:
string fileName = "test.txt";
byte[] file = File.ReadAllBytes(Server.MapPath("~/Files/" + fileName));
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
memStream.Write(file, 0, file.Length);
memStream.Seek(0, SeekOrigin.Begin);
Object obj = (Object)binForm.Deserialize(memStream);

【讨论】:

  • 您的方法正在使用 memStream,但您的代码出现错误。它说:“输入流不是有效的二进制格式。起始内容(以字节为单位)是:1F-8B-08-00-00-00-00-00-04-00-ED-BD-07- 60-1C-49-96" 在 Object obj = (Object)binForm.Deserialize(memStream) 行运行时;
  • 目前,我正在使用此代码获取文件:byte[] file; using (var stream = new FileStream(Server.MapPath("~/Files/" + fileName), FileMode.Open, FileAccess.Read)) { using (var reader = new BinaryReader(stream)) { file = reader.ReadBytes((int)stream.Length); } }
  • 如果你的文件被压缩了,我认为这是因为使用了GZipStream,那么这很可能是它不起作用的原因。
  • 我已经分析了我的代码并得出了您的想法的结论。所以我在上面发布了我的新代码以寻求更多帮助。如果是你的情况,请帮助我。
【解决方案2】:

我不确定误解在哪里。 FileStream 表示磁盘上的文件。如果不将它们写入磁盘,则无法“将字节读入其中”,而如果不从磁盘读取,则无法从其中读取。

也许你想要的是一个可以包含任意内容的 MemoryStream。

两者都派生自 Stream。

【讨论】:

  • 是的。我说过 FileStream 不能直接读取字节。如果 MemoryStream 可以包含字节数组,那么我如何将它用于我的方法“LoadFile”?
  • 要么更改LoadFile 以获取Stream,要么将byte 数组写入文件并从中创建FileStream
  • 使用两者都派生自 Stream 的事实,因此您可以传递 any 流。你即将体验继承的用处:)
  • 如果我将字节数组写入文件,然后将文件读回FileStream,那么如果文件仅存在于数据库中而不存在于服务器上的任何驱动器上,我该如何读取文件?跨度>
  • 如果您不想将字节数组写入文件来读取它,那真的是我的两个选项中最糟糕的一个。然后你所要做的就是将LoadFile 更改为Stream 而不是FileStream,然后将字节数组加载到MemoryStream 中并传递它。
【解决方案3】:

是的!现在我做了更多的研究后得到了一个很好的解决方案。作为我发布的主题“如何将字节数组读入 FileStream”。我们不能将字节数组读入 FileStream,它只是用来将驱动程序上的文件读入字节数组。所以我对我的代码进行了一些更改,现在我有一个文件可以使用 FileStream 读取它。我是如何制作文件的?

在这种情况下,我有一个对象。对象随心所欲!

我使用集合作为 samble 对象。

Collection<object> list = new Collection<object>();
//Now I will write this list to a file. fileName is what you want and be sure that folder Files is exist on server or at the root folder of your project
WriteFile(list, Server.MapPath("~/Files/" + fileName));
//The method to write object to file is here
public static void WriteFile<T>(T obj, string path)
{
    FileStream serializeStream = new FileStream(path, FileMode.Create);
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(serializeStream, obj);
    serializeStream.Flush();
    serializeStream.Close();
}

将对象写入文件后,我需要一个方法将其读回对象。所以我确实写了这个方法:

public static Collection<object> ReatFile(string fileName){
            //I have to read the file which I have wrote to an byte array            
            byte[] file;
            using (var stream = new FileStream(Server.MapPath("~/Files/" + fileName), FileMode.Open, FileAccess.Read))
            {
                using (var reader = new BinaryReader(stream))
                {
                    file = reader.ReadBytes((int)stream.Length);

                }

            }
            //And now is what I have to do with the byte array of file is to convert it back to object which I have wrote it into a file
            //I am using MemoryStream to convert byte array back to the original object.
            MemoryStream memStream = new MemoryStream();
            BinaryFormatter binForm = new BinaryFormatter();
            memStream.Write(file, 0, file.Length);
            memStream.Seek(0, SeekOrigin.Begin);
            Object obj = (Object)binForm.Deserialize(memStream);
            Collection<object> list = (Collection<object>)obj;
            return list;
}

完成上述步骤后,我现在可以将任何类型的对象写入文件,然后将其读回原始对象。非常感谢我在那里得到的任何帮助。

【讨论】:

    【解决方案4】:

    为什么在使用FileStream 之前运行File.ReadAllBytes

    string fileName = "test.txt";
    using(FileStream fs = new FileStream(Server.MapPath("~/Files/" + fileName), FileMode.Open, FileAccess.Read))
    {
        object obj = LoadFile<object>(fs);
        fs.Close();
    }
    

    【讨论】:

    • 在我的问题中,我提到字节数组没有从服务器上的任何文件中读取。它只是我从数据库或其他来源获得的一个字节数组。所以请不要将文件直接读入 FileStream。
    • 为什么是So please do not read file directly into FileStream?您正在使用Server.MapPath("~/Files/" + fileName) 读取磁盘上的文件。如果您不打算读取磁盘上的任何文件,请不要使用 FileStream。
    • 你说得对。我只有字节数组,它没有从任何文件中加载。那么你能帮我解决这个问题吗?因为“LoadFile”方法需要一个 FileStream,而我只有一个来自数据库的字节数组。那么如何将它用于我的方法“LoadFile”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多