How to read an array of bytes in a FileStream

I have a byte array and I want to read an array of bytes in a FileStream. The following is sample 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 above method, I use FileStream to read the byte array, but the unsuccessful fs.ReadByte cannot read the byte array. Any help is focused on how to read an array of bytes in a FileStream for use as a parameter in the "LoadFile" method. Please do not read the file directly in FileStream, because it downloads a file from another source, such as a database or another source.

+4
source share
4 answers
 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); 
+8
source

I do not know where this misunderstanding is. FileStream represents a file on disk. You cannot "read bytes in it" without writing them to disk, and you cannot read from it without reading from disk.

Perhaps you need a MemoryStream that can contain arbitrary content.

Both come from Stream.

+5
source

Why do you run File.ReadAllBytes before using your 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(); } 
0
source

Yes! Now I got a good solution after doing some more research. As a topic that I posted "How to read a byte array in FileStream". We cannot read the byte array in FileStream, it is simply used to read the driver file for the byte array. So I changed my code a bit and now I have a file to read it with FileStream. How did i make the file?

In this context, I have an object. An object is all you need!

I use the collection as a samble object.

 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 wrote my object to a file, I need a method to read it back to the object. Therefore, I am writing 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; } 

Having taken a few steps above, I can now write any type object to a file, and then read it back to the original object. Thanks so much for any help I received there.

0
source

All Articles