第8章-文件-改分析.ppt

第8章-文件-改分析

Directory类 解决方案: Console.WriteLine(请键入要删除的目录的路径:) ; string path = Console.ReadLine() ; if (Directory.Exists(path)) Directory.Delete(path) ; //只可删除空目录! else Console.WriteLine(目录不存在或目录非法!) ; 从控制台输入要删除的目录路径,删除目录。 bool Exists(string s) void Delete(string path) 序列化 * 序列化 把对象转换为字节序列的过程称为对象的序列化。 把字节序列恢复为对象的过程称为对象的反序列化。 序列化类BinaryFormatter Serialize(FileStreamObject, ClassObject) Deserialize(FileStreamObject) 命名空间System.Runtime.Serialization.Formatters.Binary; System.Runtime.Serialization; 示例-序列化 * [Serializable] public class Person { public string id; public string name; public int age; public char sex; public bool isMarried; public Person(string id,string name,int age,char sex,bool isMarried) { this.id = id; this.name = name; this.age = age; this.sex = sex; this.isMarried = isMarried; } } 示例-序列化 * Person p1 = new Person(001,mary,21,f,true); Person p2 = new Person(002,tom,20,m,false); FileStream fs = new FileStream(@f:\person.dat, FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, p1); formatter.Serialize(fs,p2); fs.Close(); fs = new FileStream(@f:\person.dat, FileMode.Open); Person h1 = (Person)formatter.Deserialize(fs); Person h2 = (Person)formatter.Deserialize(fs); fs.Close(); Console.Write({0},{1},{2},{3},{4}, h1.id, h1.name, h1.sex, h1.age, h1.isMarried); Console.Write({0},{1},{2},{3},{4}, h2.id, h2.name, h2.sex, h2.age, h2.isMarried); 作业: * 要求从控制台输入3位教师的工号,姓名,年龄,性别,职称,是否专职信息,并使用BinaryWriter将信息写入文件中,同时再使用BinaryReader将信息从文件中读出来。(使用对象数组) 使用序列化和反序列化的方法将3位教师的信息写入文件并读出。(使用对象数组) 学习文件与目录类,试用程序创建,删除,拷贝文件及目录 * 上机操作 文件操作 序列化 * * * 回顾 委托 事件 思考: * 设计一款射击游戏,游戏中有攻击者及被攻击者。如果攻击者发射子弹,且击中被攻击者,则被攻击者做出相应的反应表示被击中。假设游戏中的两个角色分别由两组不同的人员相互开发,且攻击者并不知道被攻击者中有什么样的函数来回应。 1.使用什么机制完成程序? 2.分别指出发送者和接收者? 3.使用程序完成。 解决方案-发送者 * class Shooter { public delegate

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档