Created
February 25, 2010 21:18
-
-
Save simi/315045 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.IO; | |
using System.Runtime.Serialization.Formatters.Binary; | |
namespace serializeArray | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int[] c = new int[(int)char.MaxValue]; | |
c[5] = 10; | |
c[6] = 20; | |
SerializeIntArray(c, "test.bin"); | |
int[] d = DeserializeIntArray("test.bin"); | |
Console.WriteLine(d[5]); | |
Console.WriteLine(d[6]); | |
} | |
public static void SerializeIntArray(int[] array, string filename) | |
{ | |
FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write); | |
BinaryFormatter sf = new BinaryFormatter(); | |
try | |
{ | |
sf.Serialize(fs, array); | |
} | |
finally | |
{ | |
fs.Close(); | |
} | |
} | |
public static int[] DeserializeIntArray(string filename) | |
{ | |
int[] arr; | |
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); | |
BinaryFormatter sf = new BinaryFormatter(); | |
try | |
{ | |
arr = (int[])sf.Deserialize(fs); | |
} | |
finally | |
{ | |
fs.Close(); | |
} | |
return arr; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment