第六讲 文件管理

第六讲    文件管理

  1. 文件和流
  2. 公共对话框

l       OpenFileDialog

l       SaveFileDialog

l       PrintDialog

l       PrintPreviewDialog

l       PageSetupDialog

l       FontDialog

l       ColorDialog

1. 文件和流

文件分类

l       程序文件和数据文件

l       程序文件是用于存放运行程序的文件;

l       数据文件是用来存储程序文件运行时需要读取或保存的数据的文件。

l         顺序文件和随机文件

    根据数据的存储形式,文件可以分为顺序文件和随机文件。

l       顺序文件也就是普通的文本文件,文件里面的数据存取方式为顺序存取,即数据是一个接一个地顺序写入文件中的。读数据时,也是一个接一个地顺序读出文件的。

l       随机文件是一种以记录形式组织数据的文件,它的数据存储方式为随机存储,可以随意读写文件中的记录数据。 但所有记录的长度必须相同。

l          文本文件和二进制文件

      根据数据的编码,文件可以分为文本文件和二进制文件。

l       文本文件是以字符方式编码和保存数据的文件; asci Unico

l       二进制文件则是以二进制方式编码和保存数据的文件。

文件访问模式

根据不同类型的文件,访问数据的方式也不相同。 VB.NET 提供了三种:

l       顺序访问模式

l       随机访问模式

l       二进制访问模式

但不论是用哪种模式访问文件,基本操作步骤是相同的,一般都须经过以下三步完成。

l       首先打开文件,如果文件不存在应先创建文件;

l       当文件打开后,就可以对文件进行读或写操作了;

l       当文件操作完毕,应该关闭文件。

 

 

 

流的概念

Stream )是字节序列的抽象概念,例如文件、输入 / 输出设备、内部进程通信管道或者 TCP/IP 套接字。

流和文件的区别:

文件是一些具有永久存储及特定顺序的字节组成的一个有序的、具有名称的集合。对于文件,一般都有相应的目录路径、磁盘存储、文件和目录名等;

流提供从存储设备写入字节和读取字节的方法,存储设备可以是磁盘、网络、内存和磁带等。

一般来说,流要比文件的范围要稍广一些,除文件流之外也存在多种流,如网络流、内存流和缓冲流等。

流的操作一般涉及三个基本方法:

读取流:读取是从流到数据结构(如字节数组)的数据传输。

写入流:写入是从数据结构到流的数据传输。

查找流:查找是对流内的当前位置进行查询和修改

其中查找功能取决于流的存储区类型 。例如,网络流没有当前位置的统一概念,因此一般不支持查找。

Stream 类及其类型(流)

Stream 类是一个抽象类 ,不能在程序中创建 Stream 的一个实例。

.NET 里面,由 Stream 派生出 5 种具体的流,分别是:

l       FileStream 支持对文件的顺序和 随机读写操作     System.IO

l       MemoryStream 支持对内存缓冲区的顺 序和随机读写操作 System.IO

l       NETworkStream 支持对 Internet 网络资源的顺序和随机读写操作。存在 于 System.Net.Sockets 名称空间。

l       CryptoStream 支持数据的编码和解码。存在于 System.Security.Cryptography 名称空间。

l       BufferedStream 支持缓冲式的读写对那些本身不支持的对象。

 

<!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0; mso-font-charset:2; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 268435456 0 0 -2147483648 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:943806538; mso-list-type:hybrid; mso-list-template-ids:-1899492600 -1840512914 -388233416 1870806592 1880127252 -1304144792 -1253806500 512122610 -257754358 1551116394;} @list l0:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Wingdings;} @list l0:level2 {mso-level-start-at:171; mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:72.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Wingdings;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

System.IO 命名空间

VB.NET 中,与 I/O 相关的类都位于命名空间 System.IO 中。使用这些,可以对流或文件进行同步或异步的读写操作。

l       System.IO 模型提供了一个基于对象的工具,用于处理文件夹和文件

l       System.IO 命名空间包含允许在数据流和文件上进行同步和异步读取及写入 的类型。

l       System.IO 命名空间中具体提供了以下功能:

    创建、删除和操作目录及文件。

    对目录和文件进行监视。

    从流中读写数据或字符。

    随机访问文件。

    使用多种枚举常量设置文件和目录的操作等。

导入 System.IO 命名空间:

         Imports System.IO

 

 

 

 

<!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0; mso-font-charset:2; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 268435456 0 0 -2147483648 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:新宋体; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@新宋体"; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:479003860; mso-list-type:hybrid; mso-list-template-ids:-408375154 -420162326 -679331892 52831724 325097020 -242470494 -1909430696 -1173713002 35943036 2124976812;} @list l0:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Wingdings;} @list l0:level2 {mso-level-start-at:171; mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:72.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Wingdings;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

FileStream

        System.IO 命名空间下除了 File 类、 Stream 类和 FileStream 类外还提供一个 BinaryReader 类,它用于从流中读取字符串或基本的数据类型。它所对应的是 BinaryWriter 类。 BinaryWriter 类用于向流中写数据。           

        进行本地文件操作的时候 ,我们可以采用 FileSteam 类, 可以很简单的读写为字节数组

       对于简单数据类型的数据的读写 ,可以采用 BinaryReader BinaryWriter 以及 StreamReader StreamWriter

l       BinaryReader ,用特定的编码将基元数据类型读作二进制值。

l       BinaryWriter 以二进制形式将基元类型写入流,并支持用特定的编码写入字符串。

l       StreamReader/Writer 则是把数据存储为 XML 格式。

File 类的常用的方法

1. 创建文件 的方法 Create

    其一般格式为:

File.Create(ByVal path As String)

2. 打开文件 的方法 Open

    其一般格式为:

File.Open(ByVal path As String, ByVal mode As FileMode ) As FileStream

3. 检查在指定的目录中是否存在某文件的方法 Exists

    其一般格式为:

File.Exists(ByVal path As String) As Boolean

4. 复制文件的方法 Copy

    其一般格式为:

File.Copy(ByVal sourceFileName As String,ByVal destFileName As String)

5. 删除指定文件的方法 Delete

    其一般格式为:

File.Delete( ByVal path As String)

6. 移动文件的方法 Move

File.Move( ByVal sourceFileName As String, ByVal destFileName As String)      

    Public Shared Sub Main()

        Dim SourceFileName As String = "d:/file.txt"

        Try

            Dim DestFileName1 As String = "d:/TEMP/FileCopy.BAK"

            Dim DestFileName2 As String = "d:/TEMP/File.txt"

            If File.Exists(DestFileName1) Then

                File.Delete(DestFileName1)

            End If

            If File.Exists(DestFileName2) Then

                File.Delete(DestFileName2)

            End If

            File.Copy(SourceFileName, DestFileName1)

            File.Move(SourceFileName, DestFileName2)

        Catch Error_Info As Exception

            Console.WriteLine(" 文件操作错误!" & Error_Info.ToString())

        End Try

    End Sub

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

2. 公共对话框

使用 InputBox MsgBox 函数可以建立简单的对话框,但同时 Visual Basic.NET 也提供了 7 个常用对话框的控件: OpenFileDialog,SaveFileDialog,FontDialog,ColorDialog,PrintDialog,PrintPreviewDialog,PageSetupDialog.

这些控件都是从 CommonDialog 类继承,下面给出 CommonDialog 类的层次结构:

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:新宋体; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@新宋体"; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

打开对话框控件( OpenFileDialog

FileDialog 类的属性方法等

OpenFileDialog1.InitialDirectory = "d:/"

设置默认打开路径

OpenFileDialog1.DefaultExt = "txt"  

设置默认文件类型

OpenFileDialog1.Filter = " 文本文件(*.txt)|*.txt"

文件过滤

DefaultExt

为一个字符串,用于获取或设置 默认文件扩展名。

FileName

用于获取或设置一个包含在文件对话框中选定的文件名的字符串。

FileNames

用于获取对话框中所有选定文件的文件名。

Multiselect

用于获取或设置一个值,该值指示对话框是否允许选择多个文件,如果对话框允许同时选定多个文件,则为 true ;反之,则为 false

RestoreDirectory

用于获取或设置一个值,该值指示对话框在关闭前是否还原当前目录

Filter

用于获取或设置当前文件名筛 选器字符串,该字符串决定对话框的 另存为文件类型 文件类型 框中出现的选择内容。

其一般格式为:

" 文件类型名称 1| 文件扩展名 1| 文件类型名称 2| 文件扩展名 2|… …| 全部文件 (*.*)|*.*"

OpenFileDialog 的基本方法

OpenFile ShowDialog 方法。

openFile 独有的一些方法和属性,主要是 OpenFile() 方法,返回 Stream 类型

OpenFile 方法:打开用户选定的具有只读权限的文件,该文件由 FileName 属性指定,其一般格式为:

       OpenFileDialog.OpenFile()

     如果运行 OpenFileDialog 对话框 ,只要使用其基本方法 ShowDialog 即可,其一般格式为:

       OpenFileDialog.ShowDialog()

如果用户在对话框中单击 确定 ,则为本方法返回 DialogResult.OK ;否则为 DialogResult.Cancel

 

 

 

 

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

保存对话框控件( SaveFileDialog

SaveFileDialog 控件与 Windows 使用的 保存文件 对话框相同。在 Windows 应用程序中可将该组件用作简单的文件保存。

OpenFileDialog 类似, SaveFileDialog 的基本属性也有 DefaultExt FileName Filter FilterIndex RestoreDirectory 等,这些属性基本性质的与 OpenFileDialog 雷同。

SaveFileDialog 的基本方法

SaveFileDialog 的基本方法是 OpenFile ShowDialog 方法。其中 OpenFile 方法打开用户选定的具有读 / 写权限的文件。其一般格式为:

SaveFileDialog.OpenFile()

该方法返回用户选定的读 / 写文件。如果使用 ShowDialog 方法将调用对话框,打开选定的文件,其一般格式为:

SaveFileDialog.ShowDialog()

如果用户在对话框中单击 确定 ,则为本方法返回 DialogResult.OK ;否则为 DialogResult.Cancel

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:新宋体; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@新宋体"; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:841.9pt 595.3pt; mso-page-orientation:landscape; margin:2.85pt 2.85pt 2.85pt 2.85pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->

  Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click

        Dim newForm As New Form2

        'newForm.MdiParent = Me

        newForm.Show()

        StatusBar1.Panels(1).Text = "NEW"

    End Sub

 

    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click

        '1 、导入相应的命名空间

        '2 、对文件和流进行操作,设置变量、

        '3 、设置如何使用OpenFileDialog

        Try

            OpenFileDialog1.InitialDirectory = "d:/"

            OpenFileDialog1.DefaultExt = "txt"

            OpenFileDialog1.Filter = " 文本文件(*.txt)|*.txt"

            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

                myFileStream = OpenFileDialog1.OpenFile

                 If Not (myFileStream Is Nothing ) Then

                    Dim buffer(myFileStream.Length()) As Byte

                    Dim myStreamReader As New StreamReader(myFileStream)

                    myStreamReader.BaseStream.Seek(0, SeekOrigin.Begin)

                    Dim strTemp As String

                    While myStreamReader.Peek() > -1

                        strTemp = strTemp & myStreamReader.ReadLine() & vbCrLf

                    End While

                    TextBox1.Text = strTemp

                     myFileStream.Close()

                End If

            End If

        Catch ex As Exception

            MessageBox.Show(ex.ToString)

        End Try

 

        StatusBar1.Panels(1).Text = "OPEN"

 

    End Sub

 

    Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click

        Try

            Dim mySaveFileDialog As New SaveFileDialog

            mySaveFileDialog.InitialDirectory = "d:/"

            mySaveFileDialog.DefaultExt = "txt"

             mySaveFileDialog.Filter = " 文本文件(*.txt)|*.txt| 所有文件(*.*)|*.*"

            mySaveFileDialog.FilterIndex = 1

            mySaveFileDialog.RestoreDirectory = True

 

            If mySaveFileDialog.ShowDialog() = DialogResult.OK Then

                Dim fileName As String

                fileName = mySaveFileDialog.FileName

                myFileStream = New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)

                Dim myStreamWriter As New StreamWriter(myFileStream)

                myStreamWriter.WriteLine(TextBox1.Text)

                myStreamWriter.Flush()

                myStreamWriter.Close()

 

            End If

 

        Catch ex As Exception

            MessageBox.Show(ex.ToString)

        End Try

    End Sub

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值