delphi 处理之文本文件


本文整理自网络,侵删。

 
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

var
  F: TextFile;
  FileName: string = 'c:/A.txt';


{$R *.dfm}

//向文本文件写入内容
procedure TForm1.Button1Click(Sender: TObject);
begin
  AssignFile(F,FileName);
  Rewrite(F);        //重写(覆盖)已存在的文件
  Writeln(F,'one');  //写入一行
  Writeln(F,'two');
  CloseFile(F);
end;

//读取文本文件内容
procedure TForm1.Button2Click(Sender: TObject);
var
  str:string;
begin
  AssignFile(F,FileName);
  Reset(F);         //以只读方式打开文本文件
  while not Eof(F) do      //循环文本文件,判断是否到尾部
  begin
    Readln(F,str);         //读取一行
    ShowMessage(str);
  end;
end;

//向文本文件追加内容
procedure TForm1.Button3Click(Sender: TObject);
begin
  AssignFile(F,FileName);
  Append(F);              //打开文件准备追加,指针指向文本末尾
  Writeln(F, 'three');   //写入一行
  Writeln(F, 'four');
  CloseFile(F);
end;

end.

相关阅读 >>

Delphi datasnap 获取客户端ip

rad studio Delphi创建安卓服务creating android services

Delphi 获得当前系统的tcp所有打开端口及ip地址

Delphi for 循环 to和downto的理解

Delphi开发linux的动态库

Delphi xe5 android在桌面添加快捷方式

Delphi 播放声音 建议采用 异步方式,比较流畅

Delphi文件是否正在使用

Delphi编程禁止用户关闭操作系统

Delphi 暴力保护进程

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...