Serialize an object to file and deserialize back in CSharp
Description
The following code shows how to serialize an object to file and deserialize back.
Example
/*ww w.j av a 2 s . c om*/
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
class MainClass
{
[STAThread]
static void Main(string[] args)
{
Point p1 = new Point();
p1.xpoint = 1;
p1.ypoint = 2;
Stream stream = File.Open("point.bin", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, p1);
stream.Close();
Stream openStream = File.Open("point.bin", FileMode.Open);
Point desierializedPoint = new Point();
desierializedPoint = (Point)bformatter.Deserialize(openStream);
Console.WriteLine(desierializedPoint);
}
}
[Serializable()]
class Point
{
public int xpoint;
public int ypoint;
}
The code above generates the following result.