资源描述:
《C#面向对象程序设计 教学课件 郑宇军 CSCH8.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第8章对象持久性—文件管理1文件和流File类复制文件替换文件移动文件删除文件File.copy("C:\a.txt","C:\b.txt");File.Replace("C:\a.txt","D:\a.txt");File.Move("C:\a.txt","D:\a.txt");File.Delete("C:\b.txt");2转义符文件和流File类时间属性GetCreationTime,GetLastAccessTime,GetLastWriteTime,Set......文档属性FileAttributesGetAttributes()Normal,A
2、chieve,Readonly,Hidden...读写文本ReadAllText,WriteAllText,ReadAllLines,WriteAllLines34Demo文本文件读写文件和流FileStream创建FileStreamfsl=File.Creat("C:\a.txt");5文件和流FileStream创建写入FileStreamfsl=File.Creat("C:\a.txt");for(bytex=1;x<=10;x++)fsl.WriteByte(x);6文件和流FileStream创建写入关闭FileStreamfsl=File.Creat("C
3、:\a.txt");for(bytex=1;x<=10;x++)fsl.WriteByte(x);fsl.close();7文件和流FileStream打开文件创建文件FileStreamfs1=File.Open("C:\a.txt",FileMode.Open);fs1=File.Open("C:\a.txt",FileMode.OpenOrCreate);fs1=File.Open("C:\a.txt",FileMode.CreateNew);fs1=File.Open("C:\a.txt",FileMode.Create);8文件不存在发生异常文件已存在发
4、生异常文件和流FileStream读写方式:Read,Write,ReadWritefs1=File.Open("C:\a.txt",FileMode.Open,FileAccess.Read);if(fs1.CanWrite)fs1.Write(newbyte[]{1,2,3,5,7,11,13});9文件和流FileStream读写位置fs1=File.Open("C:\a.txt",FileMode.Open);10head0001001B0019001D010301C802B20721......FileStream.Position文件和流FileStream
5、读写位置fs1=File.Open("C:\a.txt",FileMode.Open);fs1.ReadByte();11head0001001B0019001D010301C802B20721......FileStream.Position文件和流FileStream读写位置fs1=File.Open("C:\a.txt",FileMode.Open);fs1.ReadByte();fs1.Write(newbyte[]{1,2,4});12head0001000100020004001B0019001D010301C802B20721......FileStrea
6、m.Position文件和流FileStream读写位置fs1=File.Open("C:\a.txt",FileMode.Open);fs1.ReadByte();fs1.Write(newbyte[]{1,2,4});fs1.Seek(2);13head0001000100020004001B0019001D010301C802B20721......FileStream.Position文件和流FileStream读写位置fs1=File.Open("C:\a.txt",FileMode.Open);fs1.ReadByte();fs1.Write(newbyte
7、[]{1,2,4});fs1.Seek(-3);14head0001000100020004001B0019001D010301C802B20721......FileStream.Position15Demo流文件读写流的读写器BinaryWriter创建FileStreamfsl=File.Creat("C:\a.txt");BinaryWriterbw1=newBinaryWriter(fs1);16流的读写器BinaryWriter创建写入FileStreamfsl=File.Creat("C:\a.t