如何将字节数组读入FileStream

时间:2013-03-08 10:59:38

标签: c#

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

4 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:6)

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

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

两者都来自Stream。

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

呀!现在,我做了一些更多的研究后得到了一个很好的解作为我发布的主题“如何将字节数组读入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;
}

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

相关问题