简体   繁体   English

如何将字节数组读入FileStream

[英]How to read byte array into FileStream

I have an byte array and I want to read the byte array into a FileStream. 我有一个字节数组,我想将字节数组读入FileStream。 Below is my sample of code: 以下是我的代码示例:

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);
    }
}

In the method above, I have use FileStream to read byte array, but unlucky fs.ReadByte cannot read byte array. 在上面的方法中,我使用FileStream来读取字节数组,但不幸的是fs.ReadByte无法读取字节数组。 Any help please focus on how to Read byte array into a FileStream for using as a parameter in method "LoadFile". 任何帮助请关注如何将字节数组读入FileStream以用作方法“LoadFile”中的参数。 Please do not read directly the file into FileStream because the file here is loaded from somewhere else like from database or other source. 请不要直接将文件读入FileStream,因为此处的文件是从数据库或其他来源的其他地方加载的。

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);

I'm not sure where the misunderstanding is. 我不确定误解在哪里。 FileStream represents a file on disk. FileStream表示磁盘上的文件。 You cannot "read bytes into it" without writing them to disk and you cannot read from it without reading from disk. 如果不将磁盘写入磁盘,则无法“将字节读入其中”,如果不从磁盘读取,则无法从中读取。

Maybe what you want is a MemoryStream which can contain arbitrary contents. 也许你想要的是一个可以包含任意内容的MemoryStream。

Both derive from Stream. 两者都来自Stream。

Why do you run File.ReadAllBytes prior to the usage of your FileStream ? 为什么在使用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();
}

Yeah! 是啊! Now I got a good solution after doing some more research. 现在,我做了一些更多的研究后得到了一个很好的解 As the topic I have posted "How to read byte array into FileStream". 作为我发布的主题“如何将字节数组读入FileStream”。 We cannot read byte array into FileStream, it just use to read a file on driver to byte array. 我们无法将字节数组读入FileStream,它只是用于将驱动程序上的文件读取到字节数组。 So I have change a little bit on my code, and now I have a file to read it using FileStream. 所以我对我的代码进行了一些更改,现在我有一个文件可以使用FileStream来读取它。 How I made a file? 我是怎么做文件的?

In this context I have an object. 在这种情况下,我有一个对象。 The object is anything as you want! 对象是你想要的任何东西!

I use a collection as a samble object. 我使用集合作为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();
}

After I have wrote my object to a file, I need a method to read it back to object. 在将对象写入文件后,我需要一种方法将其读回对象。 So I do write this method: 所以我写这个方法:

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;
}

After doing some steps above, I am now can write any type object to file and then read it back to original object. 完成上面的一些步骤后,我现在可以将任何类型的对象写入文件,然后将其读回原始对象。 Thank too much for any help I have got there. 非常感谢我的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM