您的位置:首页 > 编程语言 > Delphi

Delphi文件学习二 Standard Routines and I/O

2010-08-08 22:05 197 查看
unit Unit1;

interface

uses
SysUtils, Classes, Controls, Forms,Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
VAR
fromF,ToF: file; //无类型文件
NumRead,NumWritten:Integer;
Buf : array[1..2048] of Char;
begin
if OpenDialog1.Execute then
begin
AssignFile(fromF,OpenDialog1.FileName);
Reset(fromF,1); {记录长度为1代表数据传输,只对无类型文件可用}
if SaveDialog1.Execute then
begin
AssignFile(ToF,SaveDialog1.FileName);
Rewrite(TOF,1);{记录长度为1}
Canvas.TextOut(10, 10, 'Copying ' + IntToStr(FileSize(FromF))
+ ' bytes...');
repeat
System.BlockRead(fromF,Buf,SizeOf(Buf),NumRead);
System.BlockWrite(toF,Buf,NumRead,NumWritten);
until (NumRead = 0) or (NumRead <> NumWritten);

CloseFile(FromF);
CloseFile(ToF);
Canvas.TextOut(30,30,'Success');
end;
end;

end;

end.


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
{Eof函数测试文件是否已到文件尾}
procedure TForm1.Button1Click(Sender: TObject);

var
F: TextFile;{文本文件}
Buf :String;
begin
if OpenDialog1.Execute then
begin
AssignFile(F,OpenDialog1.FileName);{邦定相关联文件}
Reset(F);{只读方式打开文件}
{EOF函数判断文件是否到尾部,如果是返回真,否为假}
while not Eof(F) do
begin
ReadLn(F,Buf);//读取文件数据
Memo1.Lines.Add(Buf);
end;
CloseFile(F);   //关闭文件

end;
end;

end.


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
F: TextFile;
begin
OpenDialog1.Title := 'Delete File';

if OpenDialog1.Execute then
AssignFile(F,OpenDialog1.FileName);
try
Reset(F);
if MessageDlg('Erase ' + OpenDialog1.FileName + '?',
mtConfirmation, [mbYes, mbNo], 0) = mrYes then

begin
CloseFile(F);
Erase(F);  {移除文件,回收站找不到}
MessageDlg('Delete the '+OpenDialog1.FileName +' Success',mtCustom,[mbOk],0);
end;
except
on EInOutError do
MessageDlg('File I/O error.', mtError, [mbOk], 0);

end;

end;

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: