1 / 24

学习目标

文件. 学习目标. 了解文件的命名空间 了解文件的概念、类型 了解流的概念 掌握文本文件的对象操作 掌握文件、文件夹的基本操作. VB.net 中的文件操作方法. 流式文件访问 文件处理函数 文件对象模型( FSO )(略). 文件. 从广义上说,文件可以看作是保存在磁盘上的二进制字节,是按一定格式存储的信息。. 文件类型. 顺序文件: 适用于纯文本文件,并假定文件中的每一个字符都表示一个文本格式设置序列。 随机文件 : 一组长度相同的记录。 二进制文件 : 可以以任何需要的形式存储数据。. System.IO 命名空间.

reba
Télécharger la présentation

学习目标

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. 文件

  2. 学习目标 • 了解文件的命名空间 • 了解文件的概念、类型 • 了解流的概念 • 掌握文本文件的对象操作 • 掌握文件、文件夹的基本操作

  3. VB.net中的文件操作方法 • 流式文件访问 • 文件处理函数 • 文件对象模型(FSO)(略)

  4. 文件 • 从广义上说,文件可以看作是保存在磁盘上的二进制字节,是按一定格式存储的信息。

  5. 文件类型 • 顺序文件: 适用于纯文本文件,并假定文件中的每一个字符都表示一个文本格式设置序列。 • 随机文件: 一组长度相同的记录。 • 二进制文件: 可以以任何需要的形式存储数据。

  6. System.IO命名空间 System.IO模型所提供的资源 ⒈ 类 包括有关文件操作的类库。主要的类有: File、Path、Directory、 File Stream等。 ⒉ 结构 WaitChangedResult:含有关于所发生的更改的信息。 ⒊ 枚举 ⑴ FileAccess:定义用于控制对文件的读访问、写访问或读/写访问的常数。 ⑵ FileAttributes:提供文件和目录的属性。 ⑶ FileMode:指定操作系统打开文件的方式。 ⑷ FileShare:含有用于控制其他FileStream对同一文件可以具有的访问类型的常数。 ⑸ NotifyFilters:指定要在文件或文件夹中监视的更改。 ⑹ SeekOrigin:指定文件存取时的相对位置。 ⑺ WatcherChangeTypes:可能会发生的文件或目录更改。

  7. System.IO命名空间功能 (括号中是提供相应功能的类) ⑴ 建立、删除以及管理文件和目录(File和Directionary)。 ⑵ 监控目录和文件的访问操作(FileSystemWatcher)。 ⑶ 对流进行单字节字符或字符块的读写操作(StreamReader和SteamWriter)。 ⑷ 对流进行多字节字符(即宽字符)的读写操作(StreamReader和SteamWriter)。 ⑸ 对流行进行字符的读写操作(StreamReader和StreamWriter)。 ⑹ 对字符串进行字符的读写操作,并允许把字符串作为字符流处理(StringReader和StringWriter)。 ⑺ 从一个流中读取数据类型和对象,或者将数据类型和对象写入流中(BinaryReader和BinaryWriter)。

  8. Stream类 • 在VB.NET中,文件、输入输出设备等都可以看作是流。归于System.IO命名空间。 • 基类是Stream类

  9. Stream类的属性 • CanRead:确定当前流是否支持读操作 • CanWrite:确定当前流是否支持定位操作 • CanSeek:确定当前流是否支持写操作 • Length:返回流的长度,以字节为单位 • Position:获取/设置当前流的读写位置操作

  10. 示例: Imports System Imports System.IO Public Class TestStream Sub test() Dim MyFileStream As New FileStream("小白杨.txt", _ FileMode.OpenOrCreate, FileAccess.Write) If MyFileStream.CanRead And MyFileStream.CanWrite Then MessageBox.Show("文件可以读写") Else If MyFileStream.CanWrite Then MessageBox.Show("文件可以写") Else MessageBox.Show("文件可以读") End If End If End Sub End Class

  11. FileStream类。 • 主要用于创建文件以及确定文件的读写操作 • FileStream 支持通过其 Seek 方法随机访问文件。默认情况下,FileStream以同步方式打开文件,但它也支持异步操作。

  12. 创建文件或打开文件 • 格式: Dim 文件对象名 As New System.IO.FileStream("文件名", 打开方式,访问方式,是否共享) • 例如: Dim Myfile As New System.io.FileStream("e:\rrrv.txt", _ IO.FileMode.OpenOrCreate, _ IO.FileAccess.Read, IO.FileShare.None)

  13. 文件的打开方式: • System.IO.FileMode.Create 文件存在则覆盖 • System.IO.FileMode.CreateNew 文件存在则出错 • System.IO.FileMode.Append 文件存在则添加的方式打开文件 • System.IO.FileMode.Open) 打开文件,文件不存在则出错

  14. 文件访问方式: • System.IO.FileAccess.Read:只写 • System.IO.FileAccess.Write:只读 • System.IO.FileAccess.ReadWrite:读写

  15. 共享权限 • System.IO.FileShare.ReadWrite • System.IO.FileShare.None • System.IO.FileShare.Read • System.IO.FileShare.Write

  16. 从流或文本中读写字符 • 字符流的读,使用StreamReader • 字符流的写,StreamWriter类。

  17. 1、读文本文件的内容示例 • 使用StreamReader类的 ReadToEnd方法将文本文件的内容读入到文本框中。 Dim file As New _ System.IO.StreamWriter("c:\test.txt") file.WriteLine("这是第一行。") file.Close()

  18. 2、将文本写入文件示例 • 使用 StreamWriter类的 WriteLine 方法将字符串写入文本文件中。 Dim file As New _ System.IO.StreamWriter("c:\test.txt") file.WriteLine(“这是第一行。”) file.Close()

  19. 3、将文本追加到文件示例 Private Sub Button7_Click(ByVal sender _ As System.Object, ByVal e As _ System.EventArgs) Handles utton7.Click ‘追加内容 Dim fs As New FileStream("e:\log.txt", FileMode.Append, FileAccess.Write) Dim SS As New StreamWriter(fs) SS.WriteLine(Now()) SS.Close() fs.Close() End Sub

  20. 从流或文本中读写基本数据类型 • BinaryReader类和 BinaryReader 类 • BinaryWriter 和 BinaryReader 类用于读取和写入数据,而不是字符串。下面的代码示例示范了向新的、空文件流 (Test.data) 写入数据及从该文件读取数据。 • 在当前目录中创建了数据文件之后,也就同时创建了相关的 BinaryWriter和 BinaryReader,BinaryWriter。用于向 Test.data 写入整数 0 到 10,Test.data 在文件尾留下了一个文件指针。在将文件指针设置回初始位置后,BinaryReader读出指定的内容。

  21. 写入数据举例: Dim FILE_NAME As String = "e:\data.txt" Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If File.Exists(FILE_NAME) = False Then Dim fs As New FileStream(FILE_NAME, FileMode.Create) fs.Close() End If Dim fs2 As New FileStream(FILE_NAME, FileMode.Append) ‘此处不可同名 ‘创建一个可写的数据文件. Dim w As New BinaryWriter(fs2) ' 写入数据. Dim i As Integer For i = 0 To 10 w.Write(CInt(i)) Next i w.Close() fs2.Close() End Sub

  22. 读数据添加到列表框中 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim fs As New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read) Dim r As New BinaryReader(fs) ' 从文件中读出数据 Dim i As Integer For i = 0 To 10 ListBox1.Items.Add(r.ReadInt32()) Next i r.Close() fs.Close() End Sub

  23. 小结: • 文件读写操作步骤: • 打开文件,使用FileStream类或其他,指定什么方式打开、创建 • 读字符文件或写字符文件,使用StreamReader,StreamWriter类 • 读写数据文件,使用BinaryReader,BinaryWriter类 • 关闭文件

  24. 作业:文本文件读写 日志文件管理 Dim mytxt As String = "C:\Documents and Settings\Administrator\My Documents\日志文件.txt" Dim myfile As New System.IO.FileStream(mytxt, IO.FileMode.Open) Dim ss As New System.IO.StreamReader(myfile) TextBox1.Text = ss.ReadToEnd myfile.Close() ‘写入日志文件 Dim myfile As New System.IO.FileStream(mytxt, IO.FileMode.Append, IO.FileAccess.Write) '添加 Dim ss As New System.IO.StreamWriter(myfile) ss.WriteLine(Now) ss.Close()

More Related