1 / 30

C# 中的文件处理

C# 中的文件处理. 目标. 讨论 System.IO 命名空间中的 各种类 讨论 C# 中的各种流处理 列出用来进行文件输入/输出的各种方法和属性 练习使用 C# 进行文件处理和其他流输入处理. IO 命名空间和其中的类. IO 命名空间包含便于在数据流和文件中读取和写入数据的类 IO 命名空间中用于处理文件的类. System.IO 命名空间. 另存为 .xls 文件. 另存为 .bmp 文件. 另存为 .txt 文件. 以字节形式向磁盘写数据 通常称为字节流。存储在磁盘上的字节集合称为文件.

darryl-mack
Download Presentation

C# 中的文件处理

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C#中的文件处理

  2. 目标 • 讨论System.IO命名空间中的各种类 • 讨论C#中的各种流处理 • 列出用来进行文件输入/输出的各种方法和属性 • 练习使用C#进行文件处理和其他流输入处理

  3. IO 命名空间和其中的类 • IO 命名空间包含便于在数据流和文件中读取和写入数据的类 • IO 命名空间中用于处理文件的类

  4. System.IO 命名空间 另存为 .xls文件 另存为 .bmp文件 另存为 .txt文件 以字节形式向磁盘写数据通常称为字节流。存储在磁盘上的字节集合称为文件

  5. BinaryReader 和 BinaryWriter • 它们都派生自System.Object类 • 这些类用于设置二进制数据的格式 • 可以从任何C#变量中读取数据并写入指定的流中 • 构造BinaryReader和BinaryWriter对象的方法是将Stream对象作为参数传递给它的构造函数,然后就可以使用BinaryReader对象的ReadXX方法和BinaryWriter对象的Write方法来读写各种类型的数据,如: FileStream objFS = new FileStream(Filename, FileMode.Create); BinaryWriter objBW = new BinaryWriter(filestream);

  6. 二进制文件读写对象 BinaryReader BinaryWriter

  7. Stream 类 • 是派生出各种类的抽象类 • 其中的一些派生类包括 • MemoryStream • BufferedStream • FileStream • NetworkStream • CryptoStream

  8. 读写内存流 抽象类 Stream 类 MemoryStream BufferedStream • 对内存而不是对磁盘进行数据读写 • 减少了对临时缓冲区和文件的需要 • 对缓冲区进行数据读写 • 允许操作系统创建自己的缓冲区 • 输入/输出效率高且速度更快 • 在网络通讯的时候经常会使用到

  9. 方法 int Read(byte[] buffer, int offset, int count) int ReadByte() void Write(byte[] buffer, int offset, int count) void WriteByte(byte value) void WriteTo(Stream stream) MemoryStream 类 • 用于从内存中读取数据和将数据写入内存中 • 以下是 MemoryStream 的一些方法

  10. BufferedStream 类 2-1 • 用于在缓冲区中读取和写入 • 它有两个重载的构造函数 • 语法 public BufferedStream(Stream StName); 默认缓冲区大小为 4096 public BufferedStream(Stream StName, int bsize); 缓冲区大小

  11. BufferedStream 类2-2 输出:

  12. 读写文本文件 3-1 System.IO 命名空间 File 类 继承类 FileStream类

  13. FileStream 类 • 用于对文件执行读写操作 • Read( )和Write( )方法用于同步读写操作 • BeginRead( )和BeginWrite( )方法用于异步读写操作 • FileStream类中的默认模式是同步读写操作

  14. 读写文本文件 3-2 • FileStream 构造函数 在构造函数中使用的 FileMode、FileAccess 和 FileShare 参数都是 enum 类型

  15. 与 FileStream类一起使用的枚举数 • FileAccess 枚举数 • Read • Write • ReadWrite • FileMode 枚举数 • Append • Create • CreateNew • Open • OpenOrCreate • Truncate • FileShare 枚举数 • None • Read • Write • ReadWrite FileStream fstream = new FileStream("Test.cs", FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.None);

  16. FileStream 类示例 创建了 FileStream 类的 一个实例,以构造具有用 户指定的名称的文件

  17. NetworkStream 类 • 用于通过网络发送和接收数据 • 位于 System.Net.Sockets命名空间中 • Read( )、ReadBytes( )、Write( )和WriteBytes( )方法用于通过网络在流和缓冲区中进行读写操作

  18. CryptoStream 类2-1 • 用于链接数据流与加密对象,以便进行数据加密 • 位于 System.Security.Cryptography命名空间中

  19. CryptoStream 类2-2 示例- 输出 该文件中的数据已 经被加密。每次执 行该程序并打开文 件 Test1.cs时,其 中的数据都以不同的 形式加密。

  20. Directory 和 File 类 • Directory类包含可用于处理目录和子目录的静态方法 • 该类的静态方法可以在没有目录实例的情况下调用 • File类包含可用于处理文件的静态方法 • 它还用于创建FileStream类

  21. 方法 Copy(string SourceFilePath, string DestinationFilePath) Create(string FilePath) Delete(string FilePath) Exists(string FilePath) Move(string SourceFilePath, string DestinationFilePath) File 类的方法

  22. Directory 和 File 类 • 示例 using System; using System.IO; class Test { static void Main(string[] args) { Directory.CreateDirectory("C#"); File.Copy("Test1.cs","C#\\Test2.cs"); Console.WriteLine("已复制文件内容"); } } 使用Directory类在当前 路径下创建了一个名为 “C#”的目录,使用File类 将文件Test1.cs的内容复 制到文件Test2.cs中

  23. FileSystemInfo 类 • 它是派生出FileInfo和 DirectoryInfo类的抽象类 • DirectoryInfo类包含可用于处理目录和子目录的方法 • FileInfo类包含可用于处理文件的方法 • DirectoryInfo 类公开实例方法 • DirectoryInfo 类的方法仅可由DirectoryInfo类的实例调用

  24. 属性 DirectoryName Extension 方法 CopyTo(string destinationfile) Create( ) Delete( ) OpenWrite( ) OpenRead( ) FileInfo 类的属性和方法

  25. 属性 FullName Parent Root 方法 Create( ) CreateSubdirectory(string directorypath) MoveTo(string destinationpath) DirectoryInfo 类的属性和方法

  26. TextReader 类 • 是 StreamReader和StringReader类的抽象基类 • 这些类可以用来读取一串有序的字符 • StreamReader从字节流中读取字符并将它转换为指定的编码 • StringReader类用于从输入字符串中读取数据

  27. 方法 Read( ) ReadLine( ) ReadToEnd( ) StreamReader 类的方法 示例

  28. TextWriter 类 • 是可用于写入有序字符的类的抽象基类 • StreamWriter和StringWriter类是TextWriter类的两个派生类 • StreamWriter以指定的编码方式向流中写入字符 • StringWriter 类用于向字符串中写入数据

  29. StreamWriter 类 • StreamWriter以指定的编码方式向流中写入字符 • 示例

  30. 总结 • IO命名空间包含便于在数据流和文件中读取和写入数据的类。 • BinaryReader和BinaryWriter类派生自System.Object。 • Stream类是派生出FileStream和MemoryStream等类的抽象类。 • FileSystemInfo类是派生出FileInfo和DirectoryInfo类的抽象类。 • TextReader是StreamReader和StringReader类的抽象基类。

More Related