txt文件日志操作类

var log:textfile;lfname:string;

 AssignFile(log,lfname); //关联文件

 Append(log);//追加文件内容

 Rewrite(log);//重写文件内容

  Writeln(log,tempstr+ str);//写入一行内容回车

  CloseFile(log); //关闭文件

//编写了一个txt文件日志操作类便于记录系统的相关运行信息

TLogFile = class
  private
    log:TextFile;
    dirPath:string;
    lfname:string;
  public
    constructor create();
    destructor destroy;override;
    procedure writeStr(str:string);
    procedure checkDir;
  end;
procedure TLogFile.checkDir;
var hd:THandle;
begin
  dirPath:=sAppRunPath+ sC_sysLog+ FormatDateTime('yyyy-mm',now);
  if not DirectoryExists(dirPath) then
  begin
    ForceDirectories(dirPath);
  end;
  lfname:= dirPath+'\'+ FormatDateTime('mm-dd',Now)+'.txt';
  if not FileExists(lfname) then
  begin
    hd:=FileCreate(lfname);
    FileClose(hd);
  end;
end;

procedure TLogFile.writeStr(str: string);
var tempStr:string;
begin
  checkDir;
  tempStr:=''+FormatDateTime('yyyy-mm-dd hh:nn:ss',Now)+'';
  AssignFile(log,lfname);
  Append(log);
  Writeln(log,tempstr+ str);
  CloseFile(log);
end;
原文地址:https://www.cnblogs.com/hejoy91/p/3105975.html