C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Info: A MemoryStream is constructed from this byte array containing the file's data.
Then: The MemoryStream is used as a backing store for the BinaryReader type, which acts upon the in-memory representation.
Byte ArrayBinaryReaderC# program that uses the MemoryStream type
using System;
using System.IO;
class Program
{
static void Main()
{
// Read all bytes in from a file on the disk.
byte[] file = File.ReadAllBytes("C:\\ICON1.png");
// Create a memory stream from those bytes.
using (MemoryStream memory = new MemoryStream(file))
{
// Use the memory stream in a binary reader.
using (BinaryReader reader = new BinaryReader(memory))
{
// Read in each byte from memory.
for (int i = 0; i < file.Length; i++)
{
byte result = reader.ReadByte();
Console.WriteLine(result);
}
}
}
}
}
Output
137
80
78
71
13
10
26
10
0
0
0
13
73
72...
Note: This consolidates resource acquisitions. It also gives you the ability to reliably use multiple streams on a single piece of data.
Locality of Reference