1 / 19

§6.1 文件和流的基本概念 “文件”是指位于磁盘上的文件和目录,通常具有名字和路径。

第六章 文件 本章主要介绍以下内容: l 文件和流的基本概念 l FileStream 类 l StreamReader 类和 StreamWriter 类 l BinaryReader 类和 BinaryWriter 类 l 目录和文件操作 l 作业. §6.1 文件和流的基本概念 “文件”是指位于磁盘上的文件和目录,通常具有名字和路径。 “流”是指提供读写后端存储的方法,后端存储包括磁盘、网络、内存等。其中和文件相关的流都继承于 Stream 抽象基类。

ramya
Download Presentation

§6.1 文件和流的基本概念 “文件”是指位于磁盘上的文件和目录,通常具有名字和路径。

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. 第六章 文件本章主要介绍以下内容:l 文件和流的基本概念l FileStream类l StreamReader类和StreamWriter类l BinaryReader类和BinaryWriter类l 目录和文件操作l作业

  2. §6.1 文件和流的基本概念 “文件”是指位于磁盘上的文件和目录,通常具有名字和路径。 “流”是指提供读写后端存储的方法,后端存储包括磁盘、网络、内存等。其中和文件相关的流都继承于Stream抽象基类。 Stream类包括文件流(FileStream)、网络流(NetworkStream)、内存数据流(MemoryStream)等,提供对字符、字节、二进制和万里格等不同数据的读写能力。 在.net framework中,与文件和流操作有关的类在命名空间System.IO中,在程序的开头需要引入这个命名空间。

  3. §6.2  FileStream类FileStream类提供文件系统标准的输入/输出。FileStream构造函数比较常用的形式如下:Public Sub New(ByVal path As String, ByVal mode As FileMode, ByVal access As _FileAccess)其中:path: 用于指定要打开(或创建)的文件的路径及文件名。Mode: FileMode枚举类型,用于指定文件打开的模式,可以是“FileMode.Create”、“FileMode.Open”、“FileMode.CreateNew”等。Access:FileAccess枚举类型,用于指定文件访问的目的,可以是“FileAccess.Read”、“FileAccess.ReadWrite”、“FileAccessWrite”例如,创建一个文件流的对象:Dim fsRW as New FileStream(“f:\file.bin”, FileMode.Open, FileAccess.Read)上述语句表示打开f:\file.bin文件,用于读出其中的内容。

  4. FileStream类常用的属性和方法

  5. §6.3  StreamReader和StreamWriter类FileStream对象只提供了字节方式的读写,StreamReader对象、StreamWriter对象分别提供字符流的读、写。StreamReader 和StreamWriter 类构造函数比较常用的形式如下:Public Sub New(ByVal Stre As FileStream)Public Sub New(ByVal Path As String)其中:Stre 用于指定读写的文件流Path用于指定文件的路径及文件名例如, 创建StreamReader类的对象:Dim fs As New FileStream(“f:\file.bin”, _ FileMode.Open, FileAccess.Read) Dim sr As New StreamReader(fs)创建StreamWriter类的对象:Dim sr As New StreamWriter(“f:\file.bin”)

  6. StreamReader常用的方法 StreamWriter常用的方法

  7. 案例讲解【案例6-1】综合使用StreamReader和StreamWriter举例案例讲解【案例6-1】综合使用StreamReader和StreamWriter举例

  8. §6.4  BinaryReader和BinaryWriter类BinaryReader对象、BinaryWriter对象分别提供了对基本数据类型的读、写方法。BinaryReader类和StreamWriter类构造函数比较常用的形式如下:Public Sub New(ByVal Stre As FileStream)其中 :Stre 用于指定读写的文件流。例如:Dim fs As New FileStream(“f:\file.bin”, FileMode.Open, FileAccess.Read)创建BinaryReader类的对象Dim sr As New BinaryReader(fs)创建BinaryWriter类的对象Dim sr As New BinaryWriter(“f:\file.bin”)

  9. BinaryReader常用的方法

  10. StreamWriter常用的方法

  11. 案例讲解 【案例6-2】综合使用BinaryReader和BinaryWriter举例

  12. §6.5  目录和文件操作Visual Basic提供了大量的方法对磁盘上的目录和文件进行操作和管理,如创建、复制、删除、移动、打开文件,创建文件夹等。一、目录操作 目录管理类主要提供的是目录的创建和删除等功能。 System.IO命名空间中包含Directory、DirectoryInfo类,二者唯一的差异是:前者提供静态(共享)方法,调用此类不需要创建对象就可以调用其提供的方法,后者提供对象实例方法。若要重复针对某个目录处理多项工作,建议使用DirectoryInfo类创建对象的实例。

  13. DirectoryInfo 常用的属性和方法

  14. Directory常用的方法

  15. 案例讲解【案例6-3】使用Directory和DirectoryInfo举例案例讲解【案例6-3】使用Directory和DirectoryInfo举例

  16. 二、文件操作 文件管理类主要提供的是文件的创建、复制、移动和删除等功能。System.IO命名空间中包含File、FileInfo类,二者提供的功能目的相同,唯一的差异是:前者提供静态方法,调用此类不需要创建对象就可以调用其提供的方法,后者提供对象实例方法。 若要重复针对某个目录处理多项工作,建议使用FileInfo类创建对象的实例。

  17. FileInfo 常用的属性和方法

  18. 案例讲解【案例6-4】使用FileInfo举例

  19. 作业:1、掌握本章基础概念2、掌握本章案例

More Related