繁体   English   中英

如何将字节数组读入FileStream

[英]How to read byte array into FileStream

我有一个字节数组,我想将字节数组读入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,因为此处的文件是从数据库或其他来源的其他地方加载的。

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

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

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

两者都来自Stream。

为什么在使用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”。 我们无法将字节数组读入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;
}

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

暂无
暂无

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

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