C#之Stream和IO
C#之Stream和IO
public class test
{
public int aa = 100;
public string testStr = "test";
public void Save(BinaryWriter bw)
{
bw.Write(aa);
bw.Write(testStr);
}
public void Load(BinaryReader br)
{
aa = br.ReadInt32();
testStr = br.ReadString();
}
}
public class Program
{
public static void Main()
{
var a = new test() { aa = 1000, testStr = "yjw" };
using(var fs=File.OpenWrite("johnyang.dat"))
using(var bw=new BinaryWriter(fs))
{
a.Save(bw);
}
var b = new test();
using(var fs=File.OpenRead("johnyang.dat"))
using(var br=new BinaryReader(fs))
{
b.Load(br);
}
Console.WriteLine(b.testStr);
Console.WriteLine(b.aa);
}
}
output:
yjw
1000
未完待续...
#####
愿你一寸一寸地攻城略地,一点一点地焕然一新
#####