var
S:String;
Begin
S:=edt1.Text+'|'+edt2.Text+'|'+edt3.Text+'|'+edt4.Text+'|'+edt5.Text+'|'+edt6.Text+'|'+edt7.Text;
End;使用TIME控件将S实时写入文本文件,并且写入一次即换行继续写入下一行。求相关代码和实例!PS:TXT路径和程序在同一目录
S:String;
Begin
S:=edt1.Text+'|'+edt2.Text+'|'+edt3.Text+'|'+edt4.Text+'|'+edt5.Text+'|'+edt6.Text+'|'+edt7.Text;
End;使用TIME控件将S实时写入文本文件,并且写入一次即换行继续写入下一行。求相关代码和实例!PS:TXT路径和程序在同一目录
procedure TForm1.Timer1Timer(Sender: TObject);
var
AFile: TextFile;
AFilePath,AStr: string;
begin
AStr := 'sdfsdf';
AFilePath := extractfilepath(Paramstr(0))+'Myfile.txt';
AssignFile(AFile,AFilePath);
Append(AFile);
Writeln(AFile,AStr);
CloseFile(AFile);
end;
//添加到程序中,设置保存路径后即可使用
unit LogWriter;interfaceuses
SysUtils, Classes;type
TLogWriter = class(TComponent)
private
FLogFileName: string;
FLogFile: Text;
FSingleLine: boolean;
FRecordTime: Boolean;
FReserved: integer;
FMaxCountPreFile: integer;
FLogFileNamePrefix: string;
FLogFilePath: string;
FLogFileNameSuffix: string;
FDisplay: TStrings;
FAddBlankLine: Boolean;
FFlushImmediately: Boolean;
procedure SetLogFilePath(const Value: string);
procedure SetLogFileNamePrefix(const Value: string);
procedure SetMaxCountPreFile(const Value: integer);
procedure SetRecordTime(const Value: Boolean);
procedure SetReserved(const Value: integer);
procedure SetSingleLine(const Value: boolean);
procedure SetLogFileNameSuffix(const Value: string);
procedure SetDisplay(const Value: TStrings);
procedure SetAddBlankLine(const Value: Boolean);
procedure SetFlushImmediately(const Value: Boolean); { Private declarations }
protected
{ Protected declarations }
FLogLineCounter: integer;
procedure ListSorted(p_List: TStrings);
procedure BuildLogFile;
procedure DelLogFile;
procedure _FindAllFiles(p_Path, p_FileExt: string; p_FindSubPath: Boolean; p_Names: TStrings);
public
{ Public declarations }
procedure AddLog(p_Info: string);
constructor Create(Owner: TComponent); override;
procedure FlushLogFile;
destructor Destroy; override;
published
{ Published declarations }
property LogFilePath: string read FLogFilePath write SetLogFilePath;
property LogFileNamePrefix: string read FLogFileNamePrefix write SetLogFileNamePrefix; //日志文件名前缀
property LogFileNameSuffix: string read FLogFileNameSuffix write SetLogFileNameSuffix;
property MaxCountPreFile: integer read FMaxCountPreFile write SetMaxCountPreFile; //每个文件最大记录数
property Reserved: integer read FReserved write SetReserved;
property SingleLine: boolean read FSingleLine write SetSingleLine; //每行一条记录
property RecordTime: Boolean read FRecordTime write SetRecordTime; //自动添加时间
property Display: TStrings read FDisplay write SetDisplay;
property AddBlankLine: Boolean read FAddBlankLine write SetAddBlankLine; //在日志信息后添加空白行
property FlushImmediately: Boolean read FFlushImmediately write SetFlushImmediately;
end;procedure Register;implementation
{$R LogWriter.dcr}procedure Register;
begin
RegisterComponents('LimEX', [TLogWriter]);
end;{ TLogWriter }procedure TLogWriter.AddLog(p_Info: string);
var
tmpStr: string;
begin
if FLogFilePath = '' then exit;
if assigned(FDisplay) then
FDisplay.add(p_Info); if FLogLineCounter > FMaxCountPreFile then
begin
closeFile(FLogFile);
buildLogFile;
end;
try
tmpStr := '';
if RecordTime then
tmpStr := formatdatetime('yyyy-mm-dd hh:nn:ss.zzz', now);
if not SingleLine then
tmpStr := tmpStr + #13#10;
tmpStr := tmpStr + p_Info;
if AddBlankLine then
tmpStr := tmpStr + #13#10;
writeln(FLogFile, tmpStr);
if FFlushImmediately then
flush(FLogFile);
inc(FLogLineCounter);
except
end;
end;procedure TLogWriter.BuildLogFile;
var
tmpBeginRecTime: string;
begin
FLogLineCounter := 0;
tmpBeginRecTime := formatdatetime('yyyymmdd_hhnnss', now);
AssignFile(FLogFile, format('%s%s_%s.LOG', [FLogFilePath, FLogFileNamePrefix, tmpBeginRecTime]));
Rewrite(FLogFile);
end;
constructor TLogWriter.Create(Owner: TComponent);
begin
inherited;
MaxCountPreFile := 100000;
Reserved := 30;
FSingleLine := false;
FRecordTime := true;
FLogFilePath := '';
FLogFileNameSuffix := '.LOG';
FLogFileNamePrefix := 'HRH';
FAddBlankLine := true;
FFlushImmediately := false;
end;procedure TLogWriter.DelLogFile;
var
i: integer;
tmpFileName: string;
tmpDatestr: string;
tmpDate: TDateTime;
tmpList: TStrings;
begin
tmpList := TStringList.Create; _FindAllFiles(FLogFilePath, '.log', false, tmpList);
ListSorted(tmpList); for i := Reserved to tmpList.Count - 1 do
begin
deletefile(tmpList[i]);
end;
end;
destructor TLogWriter.Destroy;
begin
try
close(FLogFile);
except
end;
inherited;
end;procedure TLogWriter.FlushLogFile;
begin
flush(FLogFile);
end;procedure TLogWriter.ListSorted(p_List: TStrings);
var
i, j: integer;
tmpStr: string;
begin
for i := 0 to p_List.Count - 1 do
begin
tmpStr := p_List[i];
for j := i to p_List.Count - 1 do
begin
if StrComp(pchar(tmpStr), pchar(p_List[j])) < 0 then
begin
tmpStr := p_List[j];
p_List[j] := p_List[i];
p_List[i] := tmpStr;
end;
end;
end;
end;procedure TLogWriter.SetAddBlankLine(const Value: Boolean);
begin
FAddBlankLine := Value;
end;procedure TLogWriter.SetDisplay(const Value: TStrings);
begin
FDisplay := Value;
end;procedure TLogWriter.SetFlushImmediately(const Value: Boolean);
begin
FFlushImmediately := Value;
end;procedure TLogWriter.SetLogFileNamePrefix(const Value: string);
begin
FLogFileNamePrefix := Value;
end;procedure TLogWriter.SetLogFileNameSuffix(const Value: string);
begin
FLogFileNameSuffix := Value;
end;procedure TLogWriter.SetLogFilePath(const Value: string);
begin
FLogFilePath := IncludeTrailingBackslash(Value);
DelLogFile;
BuildLogFile;
end;procedure TLogWriter.SetMaxCountPreFile(const Value: integer);
begin
FMaxCountPreFile := Value;
end;procedure TLogWriter.SetRecordTime(const Value: Boolean);
begin
FRecordTime := Value;
end;procedure TLogWriter.SetReserved(const Value: integer);
begin
FReserved := Value;
end;procedure TLogWriter.SetSingleLine(const Value: boolean);
begin
FSingleLine := Value;end;procedure TLogWriter._FindAllFiles(p_Path, p_FileExt: string;
p_FindSubPath: Boolean; p_Names: TStrings);
var
tmpDirLst: TStrings;
sr: TSearchRec;
FileAttrs: Integer;
tmpFilePath: string;
tmpFileExt: string;
i: integer;
begin
tmpDirLst := TStringList.Create;
tmpFileExt := uppercase(ExtractFileExt(p_FileExt));
tmpFilePath := ExtractFilePath(p_Path);
//FileAttrs := faReadOnly + faHidden + faSysFile + faVolumeID + faDirectory + faArchive + faAnyFile;
FileAttrs := faAnyFile;
if FindFirst(tmpFilePath + '*.*', FileAttrs, sr) = 0 then
begin
if (sr.Attr and FileAttrs) = sr.Attr then
begin
if (sr.Attr and faDirectory) > 0 then //文件夹
begin
if (sr.Name <> '.') and (sr.Name <> '..') then
tmpDirLst.Add(tmpFilePath + sr.Name + '\');
end
else //文件
begin
//if sr.Size > 0 then //不为空的文件
begin
if tmpFileExt <> '' then
begin
if uppercase(ExtractFileExt(sr.Name)) = tmpFileExt then
begin
p_Names.Add(tmpFilePath + sr.Name);
end;
end
else
p_Names.Add(tmpFilePath + sr.Name);
end;
end;
end;
while FindNext(sr) = 0 do
begin
if (sr.Attr and FileAttrs) = sr.Attr then
begin
if (sr.Attr and faDirectory) > 0 then //文件夹
begin
if (sr.Name <> '.') and (sr.Name <> '..') then
tmpDirLst.Add(tmpFilePath + sr.Name + '\');
end
else //文件
begin
//if sr.Size > 0 then //不为空的文件
begin
if tmpFileExt <> '' then
begin
if uppercase(ExtractFileExt(sr.Name)) = tmpFileExt then
begin
p_Names.Add(tmpFilePath + sr.Name);
end;
end
else
p_Names.Add(tmpFilePath + sr.Name);
end;
end;
end;
end;
FindClose(sr);
end;
if p_FindSubPath then
begin
for i := 0 to tmpDirLst.Count - 1 do
begin
_FindAllFiles(tmpDirLst[i], p_FileExt, p_FindSubPath, p_Names);
end;
end;
tmpDirLst.Free;
end;
end.
//调用方法
procedure Tfrm_Main.FormCreate(Sender: TObject);
begin
LogWriter1.LogFilePath := AppPath; LogWriter1.AddLog(p_Info);
end;
write是在紧跟文字后面写procedure TForm1.Button1Click(Sender: TObject);
var
LogFile: TextFile;
begin
if not FileExists('c:\Log1.txt') then
begin
AssignFile(LogFile, 'c:\Log1.txt');
Rewrite(LogFile);
CloseFile(LogFile); //关闭时自动保存文件
end;
AssignFile(LogFile, 'c:\Log1.txt');
Append(LogFile); writeln(LogFile);
writeln(LogFile, '*********End**********');
writeln(LogFile);
writeln(LogFile, '********begin*********');
Write(LogFile, 'Begin the same line');
Write(LogFile, '//the same line');
CloseFile(LogFile);
end;