V4_1.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:2k
- unit V4_1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
- type
- TForm1 = class(TForm)
- btnCreate: TButton;
- btnAppend: TButton;
- btnOpenRead: TButton;
- Memo1: TMemo;
- procedure btnCreateClick(Sender: TObject);
- procedure btnAppendClick(Sender: TObject);
- procedure btnOpenReadClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.btnCreateClick(Sender: TObject);
- var
- MyTextFile: TextFile;
- s: string;
- i: integer;
- begin
- //关联文件
- AssignFile(MyTextFile, 'MyTextFile.txt');
- //覆盖或创建新文件
- Rewrite(MyTextFile);
- //向文本文件里写字符串
- try
- for i := 1 to 5 do
- begin
- s := 'This is line #';
- Writeln(MyTextFile, s, i);
- end;
- finally
- CloseFile(MyTextFile);
- end;
- end;
- procedure TForm1.btnAppendClick(Sender: TObject);
- var
- MyTextFile: TextFile;
- s: string;
- i: integer;
- begin
- //关联文件
- AssignFile(MyTextFile, 'MyTextFile.txt');
- //判断文件是否存在
- if FileExists('MyTextFile.txt') then
- //以添加的方式打开文本
- Append(MyTextFile)
- else
- raise Exception.Create('MyTextFile.txt不存在');
- //向文本添加字符串
- try
- for i := 6 to 10 do
- begin
- s := 'This is line #';
- Writeln(MyTextFile, s, i);
- end;
- finally
- CloseFile(MyTextFile);
- end;
- end;
- procedure TForm1.btnOpenReadClick(Sender: TObject);
- var
- MyTextFile: TextFile;
- s: string[14];
- i, j: integer;
- begin
- //关联文件
- AssignFile(MyTextFile, 'MyTextFile.txt');
- //判断文件是否存在
- if FileExists('MyTextFile.txt') then
- //以只读方式打开文本
- Reset(MyTextFile)
- else
- raise Exception.Create('MyTextFile.txt不存在');
- //读取文本数据
- try
- while not eof(MyTextFile) do
- begin
- Readln(MyTextFile, s, j);
- Memo1.lines.Add(s + IntToStr(j));
- end;
- finally
- CloseFile(MyTextFile);
- end;
- end;
- end.