Read the data from a file and desiralize it : Binary Read Write « File Stream « C# / C Sharp






Read the data from a file and desiralize it

  
/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 * Version: 1
 */
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Client.Chapter_11___File_and_Streams
{
  public class Class1Chapter_11___File_and_Streams {
    [STAThread]
    static void Main(string[] args)
    {
      Point p1 = new Point();

      p1.xpoint = 0x1111;
      p1.ypoint = 0x2222;

      // Opens a file and serializes the object into it.
      Stream stream = File.Open("onepoint.bin", FileMode.Create);
      BinaryFormatter bformatter = new BinaryFormatter();

      bformatter.Serialize(stream, p1);
      stream.Close();

      //Read the data from a file and desiralize it
      Stream openStream = File.Open("onepoint.bin", FileMode.Open);
      Point desierializedPoint = new Point();

      desierializedPoint = (Point)bformatter.Deserialize(openStream);
    }
  }
  [Serializable()]
  class Point
  {
    public int xpoint;
    public int ypoint;
  }

}

           
         
    
  








Related examples in the same category

1.Binary Writer Reader
2.new BinaryReader(stream)
3.StreamWriter and BinaryWriter
4.new BinaryWriter(stream) and Write method
5.Creating a sequential-access file
6.Reading a sequential-access fileReading a sequential-access file
7.BinaryWriter and BinaryReader classes for the writing and reading of primitive typesBinaryWriter and BinaryReader classes for the writing and reading of primitive types
8.Working with the BinaryWriter Class
9.Working with the BinaryReader ClassWorking with the BinaryReader Class
10.File pointer move and read binary file
11.Write and then read back binary dataWrite and then read back binary data
12.Use BinaryReader and BinaryWriter to implement a simple inventory programUse BinaryReader and BinaryWriter to implement 
   a simple inventory program
13.Read and Write a Binary File
14.Read Struct out of a file with BinaryReader
15.extends BinaryReader
16.Write a value at a given position. Used to write a size of data in an earlier located header.