3.文件编程

Delphi XE8中,文件包括:文本文件、有类型数据文件、无类型数据文件。这里的文件仅指磁盘文件。我们会在接下来的章节中逐步探讨各种文件的操作。本节主要针对适用于所有文件的基本操作过程及函数进行介绍,主要包括:关联文件、打开文件、关闭文件、删除文件等基本操作。

3.1关联文件

关联文件是指将文件变量与外部文件之间建立联系,表示后面在程序中通过文件变量来对文件进行操作。关联文件的过程定义格式:

procedure AssignFile(var F: File; FileName: String);

其中:

调用该过程时,如果文件名参数为空,文件变量将与标准输入输出文件建立关联。对于已经打开的文件变量,不要再使用该过程调用。

3.2打开文件

要进行文件的读写操作必须先打开文件。

1.以读方式打开文件

通过调用 Reset 过程打开一个已经存在的文件。格式:

procedure Reset(var F: File; [RecSize: Word]);

其中:

如果指定的文件不存在,会产生错误;如果文件已经打开,则会先关闭文件然后打开。

打开文件后,文件的操作位置设置在文件的开始。

如果文件是空文件,则 Eof(F) 返回值为 True,否则为 False。

2.以写方式打开文件

通过调用 Rewrite 过程可创建并打开一个新文件。格式:

procedure Rewrite(var F: File; [RecSize: Word]);

参数同 Reset 过程。

如果存在一个同名的文件,则删除原文件后生成新的空文件;如果文件已经打开,则先关闭后重新创建。

打开文件后,文件的操作位置设置在文件的开始。

Eof(F) 返回值一定是 True。

3.3关闭文件

当对文件操作完成后,必须调用关闭文件的操作。关闭文件使用 CloseFile 过程,调用该过程后,文件变量与磁盘文件的关联中断。格式:

procedure CloseFile(var F: File);

调用该过程后,系统将释放文件变量并关闭关联的外部文件。

3.4删除文件

当需要删除一个文件时,可以调用 Erase 过程,格式:

procedure Erase(var F: File);

调用该过程将删除与文件变量 F 关联的文件,在删除文件前会执行关闭文件的操作。

3.5文件的常用基本操作

function IOResult: Integer;

procedure Rename(var F: File; NewName: String); procedure Rename(var F: File; NewName: PChar);

procedure ChDir(s: Stirng);

function Eof(var F: File): Boolean; function Eof(var F: Text): Boolean;

procedure GetDir(D: Byte; var S: String);

其中:D表示驱动器,取值为0,1,2,3...,分别代表A,B,C,D,...;

procedure RmDir(s: Stirng);

procedure MkDir(s: String);

function DeleteFile(const FileName:string):Boolean;

3.6文件编程示例

示例:编写一个简单的文件管理器,界面如下图:

编程操作(9.键盘鼠标和文件编程)(1)

新建目录界面:

编程操作(9.键盘鼠标和文件编程)(2)

重命名界面:

编程操作(9.键盘鼠标和文件编程)(3)

本例涉及三个窗体:

主窗体主要组件的属性设置如下:

对象

属性

属性值

说明

DriveComboBox1

DirList

DirectoryListBox1

链接到DirectoryListBox1

DirectoryListBox1

FileList

FileListBox1

链接到FileListBox1

DirLabel

Label1

链接到Label

FilterComboBox1

FileList

FileListBox1

链接到FileListBox1

FileListBox1

MultiSelect

False

单选

Button1

Caption

新建目录

标题

Button2

Caption

删除目录

标题

Button3

Caption

删除文件

标题

Button4

Caption

重命名

标题

代码如下:

主窗体代码:

uses Unit2, Unit3; procedure TForm1.Button1Click(Sender: TObject); begin // 新建目录 if Form2.ShowModal = mrOk then DirectoryListBox1.Update; end; procedure TForm1.Button2Click(Sender: TObject); var DirName: String; i: Integer; begin // 删除目录 for i := 0 to DirectoryListBox1.Items.Count - 1 do begin if DirectoryListBox1.Selected[i] then begin DirName := DirectoryListBox1.Items.Strings[i]; RmDir(DirName); end; end; DirectoryListBox1.Update; end; procedure TForm1.Button3Click(Sender: TObject); var FileName: String; i: Integer; begin // 删除文件 for i := 0 to FileListBox1.Items.Count - 1 do begin if FileListBox1.Selected[i] then begin FileName := FileListBox1.Items.Strings[i]; FileName := DirectoryListBox1.Directory '\' FileName; DeleteFile(FileName); end; end; FileListBox1.Update; end; procedure TForm1.Button4Click(Sender: TObject); var FileName: String; i: Integer; begin // 重命名 for i := 0 to FileListBox1.Items.Count - 1 do begin if FileListBox1.Selected[i] then begin FileName := FileListBox1.Items.Strings[i]; end; end; if FileName <> '' then Form3.FileName := FileName; if Form3.ShowModal = mrOk then FileListBox1.Update; end;

新建目录窗体代码:

procedure TForm2.Button1Click(Sender: TObject); begin // 取消按钮 ModalResult := mrCancel; end; procedure TForm2.Button2Click(Sender: TObject); begin // 确定按钮,在当前目录下创建目录 if Edit1.Text = '' then ShowMessage('请输入目录名') else begin MkDir(Edit1.Text); ModalResult := mrOk; end; end;

重命名窗体代码:

interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm3 = class(TForm) Label1: TLabel; Edit1: TEdit; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } FileName: String; end; var Form3: TForm3; implementation {$R *.dfm} procedure TForm3.Button1Click(Sender: TObject); begin // 取消 ModalResult := mrCancel; end; procedure TForm3.Button2Click(Sender: TObject); var F: File; begin // 确定 if Edit1.Text = '' then ShowMessage('请输入新的文件名!') else begin AssignFile(F, FileName); Rename(F, Edit1.Text); end; ModalResult := mrOk; end;

在重命名窗体中,interface 部分类 TForm3 中定义了一个公共属性:FileName: String; 用于在调用该窗体时传递要重命名的文件名,在主窗体中调用 TForm3 时的代码:

Form3.FileName := FileName; if Form3.ShowModal = mrOk then FileListBox1.Update;

,