在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Delphi 支持三种文件类型: 文本文件、记录文件、无类型文件。
//读写示例: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; Button6: TButton; Button7: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button6Click(Sender: TObject); procedure Button7Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} var F: Text; //TextFile 和 Text 是一样的 FileName: string = 'c:\temp\test.txt'; //写文件 procedure TForm1.Button1Click(Sender: TObject); begin AssignFile(F,FileName); Rewrite(F); //会覆盖已存在的文件 Writeln(F, '第一行'); Writeln(F, '第二行'); CloseFile(F); end; //读文件(先执行写文件) procedure TForm1.Button2Click(Sender: TObject); var s: string; begin AssignFile(F,FileName); Reset(F); //只读打开 Readln(F, s); //读取 ShowMessage(s); //显示: 第一行 Readln(F, s); //继续读取 ShowMessage(s); //显示: 第二行 CloseFile(F); end; //追加 procedure TForm1.Button3Click(Sender: TObject); begin AssignFile(F,FileName); Append(F); //打开准备追加 Writeln(F, '第三行'); Writeln(F, '第四行'); CloseFile(F); end; //读取全部(需要Memo帮忙显示) procedure TForm1.Button4Click(Sender: TObject); var s: string; begin AssignFile(F,FileName); Reset(F); //只读打开 Memo1.Clear; while not Eof(F) do begin Readln(F, s); Memo1.Lines.Add(s); end; CloseFile(F); end; //分列写入 procedure TForm1.Button5Click(Sender: TObject); //这个过程级函数的功能是: 用空格把 s 凑够 n 的长度 function AddSpace(s: string; n: Word): string; begin while Length(s) < n do begin s := s + ' '; end; Result := s; end; var name: string[8]; address: string[16]; begin AssignFile(F, FileName); Rewrite(F); name := '张三'; name := AddSpace(name,8); address := '山东泰安'; address := AddSpace(address,16); Writeln(F, name, address); name := '李四儿'; name := AddSpace(name,8); address := '贵州省安顺黄果树'; address := AddSpace(address,16); Writeln(F, name, address); name := '王二麻子'; name := AddSpace(name,8); address := '北京市海淀区'; address := AddSpace(address,16); Writeln(F, name, address); CloseFile(F); {写入的结果是: 张三 山东泰安 李四儿 贵州省安顺黄果树 王二麻子北京市海淀区 } end; //分列读取 procedure TForm1.Button6Click(Sender: TObject); var name: string[8]; address: string[16]; begin AssignFile(F, FileName); Reset(F); Memo1.Clear; while not Eof(F) do begin Readln(F, name); Memo1.Lines.Add(name); end; CloseFile(F); Reset(F); while not Eof(F) do begin Readln(F, name, address); Memo1.Lines.Add(address); end; CloseFile(F); {读取结果: 张三 李四儿 王二麻子 山东泰安 贵州省安顺黄果树 北京市海淀区 } end; //不同数据类型的写入和读取 procedure TForm1.Button7Click(Sender: TObject); var name: string[6]; age: Word; birthday: TDate; begin AssignFile(F,FileName); Rewrite(F); name := '菜花 '; //加两个空格凑够6个字符 age := 18; birthday := StrToDate(DateToStr(Now-18*365)); //假如她今天过生日 Writeln(F,name,age,birthday); CloseFile(F); Reset(F); Readln(F,name,age,birthday); Memo1.Clear; Memo1.Lines.Add(name); Memo1.Lines.Add(IntToStr(age)); Memo1.Lines.Add(DateToStr(birthday)); CloseFile(F); {读取结果: 菜花 18 1989-12-23 } //其实这样的东西应该用类型文件操作更合适, 但如果有这样的文本文件让你读取呢? end; end. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论