Home
C#
Dictionary Binary File
This page was last reviewed on Nov 17, 2022.
Dot Net Perls
Dictionary, binary file. A C# Dictionary can be stored in a binary file. We develop a custom file format that allows for efficient persistence.
Convert Dictionary
Dictionary
Important types. By combining FileStream, BinaryReader and BinaryWriter, we can write a Dictionary to the disk. This approach is not the only possible one, but it works.
BinaryWriter
BinaryReader
Example. The binary file contains a single 32-bit integer at the start. This tells us how many pairs are found in the rest of the file. Then we can read in that many pairs.
Detail Here you are asked to either Write the Dictionary and its contents to the file, or Read in a Dictionary from that file.
Console.ReadLine
Detail In Write, we use the using statement on a FileStream and BinaryWriter. We implement the simple file format described.
Detail In Read, we use a FileStream and BinaryReader and then loop through the required number of pairs.
using System; using System.Collections.Generic; using System.IO; class Program { static void Main() { while (true) { Console.WriteLine("1=Write, 2=Read"); string value = Console.ReadLine(); if (value == "1") { var dictionary = new Dictionary<string, string>(); dictionary["perls"] = "dot"; dictionary["net"] = "perls"; dictionary["dot"] = "net"; Write(dictionary, "C:\\dictionary.bin"); } else if (value == "2") { var dictionary = Read("C:\\dictionary.bin"); foreach (var pair in dictionary) { Console.WriteLine(pair); } } } } static void Write(Dictionary<string, string> dictionary, string file) { using (FileStream fs = File.OpenWrite(file)) using (BinaryWriter writer = new BinaryWriter(fs)) { // Put count. writer.Write(dictionary.Count); // Write pairs. foreach (var pair in dictionary) { writer.Write(pair.Key); writer.Write(pair.Value); } } } static Dictionary<string, string> Read(string file) { var result = new Dictionary<string, string>(); using (FileStream fs = File.OpenRead(file)) using (BinaryReader reader = new BinaryReader(fs)) { // Get count. int count = reader.ReadInt32(); // Read in all pairs. for (int i = 0; i < count; i++) { string key = reader.ReadString(); string value = reader.ReadString(); result[key] = value; } } return result; } }
1=Write, 2=Read 1 1=Write, 2=Read 2 [perls, dot] [net, perls] [dot, net] 1=Write, 2=Read
Output details. You can see the Dictionary was written to the file. The Dictionary instances are separate in Main. I checked the C:\dictionary.bin file that was used.
And The file was 32 bytes, which means the characters were encoded with one byte each.
Discussion. There are advantages to using a custom binary format to persist a collection. If you are using integers instead of strings, a binary format can be used to efficiently encode values.
Also Performance reading in the file is good. It doesn't require much parsing. The reads are sequential and thus exploit the cache.
Summary. A Dictionary can be written to and read from a binary file. We developed a simple file format that stores the number of pairs in the first integer in the binary file.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 17, 2022 (simplify).
Home
Changes
© 2007-2024 Sam Allen.