Under certain circumstances, you may need to save multiple objects to a file. Internet looking for a while, I did not find specifically say this issue. Bo asked to see a related question and answer, finishing a bit. The following is one of my simple realization:
// test for saving class
[the Serializable]
class TestToSerizable { public int i; public string b; public TestToSerizable(int i, string b) { this.i = i; this.b = b; } } class Program { static void Main(string[] args) { TestToSerizable testa = new TestToSerizable(1, "testa"); TestToSerizable testb = new TestToSerizable(2, "testb"); TESTC TestToSerizable = new new TestToSerizable ( . 3 , " TESTC " );
// save the sequence of a plurality of objects FileStream fs = new FileStream("DataFile.dat", FileMode.OpenOrCreate); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, testa); formatter.Serialize(fs, testb); formatter.Serialize(fs, testc); fs.Flush(); fs.Close();
// read the plurality of objects deserializing FS = new new the FileStream ( " DataFile.dat " , FileMode.Open); formatter = new BinaryFormatter(); TestToSerizable a = (TestToSerizable)formatter.Deserialize(fs); TestToSerizable b = (TestToSerizable)formatter.Deserialize(fs); TestToSerizable c = (TestToSerizable)formatter.Deserialize(fs); fs.Close();
Console.WriteLine("a.i=" + a.i + " a.b=" + a.b);
Console.WriteLine("b.i=" + b.i + " b.b=" + b.b);
Console.WriteLine("c.i=" + c.i + " c.b=" + c.b);
}
}
operation result:
Further, there may be different types of objects you want to save the situation.
[Serializable] class TestToSerizableA { public int i; public string b; public TestToSerizableA(int i, string b) { this.i = i; this.b = b; } } [Serializable] class TestToSerizableB { public bool a; public float b; public TestToSerizableB(bool a, float b) { this.a = a; this.b = b; } } class Program { static void Main(string[] args) { TestToSerizableA testa = new TestToSerizableA(1, "testa"); TestToSerizableB testb = new TestToSerizableB ( false , 1.23f ); TestToSerizableA testc = new TestToSerizableA(3, "testc"); TestToSerizableB testd = new TestToSerizableB ( true , 3.14f ); // objects of different types of cross saved the FileStream FS = new new the FileStream ( " DataFile.dat " , FileMode.OpenOrCreate); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, testa); formatter.Serialize(fs, testb); formatter.Serialize(fs, testc); formatter.Serialize(fs, testd); formatter.Serialize (FS, . 1 ); // basic type may be fs.Flush(); fs.Close(); // sequentially read during storage FS = new new the FileStream ( " DataFile.dat " , FileMode.Open); formatter = new BinaryFormatter(); TestToSerizableA a = (TestToSerizableA)formatter.Deserialize(fs); TestToSerizableB b = (TestToSerizableB) formatter.Deserialize (fs); TestToSerizableA c = (TestToSerizableA)formatter.Deserialize(fs); TestToSerizableB d = (TestToSerizableB)formatter.Deserialize(fs); int i = (int)formatter.Deserialize(fs); fs.Close(); Console.WriteLine("a.i=" + a.i + " a.b=" + a.b); Console.WriteLine("b.i=" + b.a + " b.b=" + b.b); Console.WriteLine("c.i=" + c.i + " c.b=" + c.b); Console.WriteLine("d.i=" + d.a + " d.b=" + d.b); Console.WriteLine("i=" + i);
}
}
operation result:
The first blog, start a discussion, please predecessors wing.
the above
Reproduced in: https: //www.cnblogs.com/zhang-ming/p/3793213.html