// 很多办法可以写一个文件:),下面就是其中一个:)
Procedure Write_log(LogMsg:string);
var
Strings: TStrings;
begin
Strings:=TStringList.Create;
Strings.Add(Formatdatetime('yyyy-mm-dd hh:mm:ss',now) + ' ' + LogMsg);
Strings.SaveToFile(GetCurrentDir+ '/PhoneLog.txt');
Strings.Free;
end;
//下面是另一个:
Procedure Write_log2(LogMsg:string);
var
Stream: TStream;
s: string;
begin
Stream:=TFileStream.Create(GetCurrentDir+ '/PhoneLog.txt',fmCreate);
s:=Formatdatetime('yyyy-mm-dd hh:mm:ss',now) + ' ' + LogMsg;
Stream.WriteBuffer(s[1],length(s));
Stream.Free;
end;
// Pascal 的传统方式则是这样:
Procedure Write_log3(LogMsg:string);
var
F: TextFile;
begin
AssignFile(F,GetCurrentDir+ '/PhoneLog.txt');
Rewrite(F);
Writeln(Formatdatetime('yyyy-mm-dd hh:mm:ss',now) + ' ' + LogMsg);
CloseFile(F);
end;
// 如果你想要的是接着把文件写下去,而不是每次写新的内容,那么应该改成:
Procedure Write_log_1(LogMsg:string);
var
Strings: TStrings;
fn: string;
begin
fn:=GetCurrentDir+ '/PhoneLog.txt';
Strings:=TStringList.Create;
if FileExists(fn) then
Strings.LoadFromFile(fn);
Strings.Add(Formatdatetime('yyyy-mm-dd hh:mm:ss',now) + ' ' + LogMsg);
Strings.SaveToFile(fn);
Strings.Free;
end;
//下面是另一个:
Procedure Write_log2_1(LogMsg:string);
var
Stream: TStream;
fn,s: string;
begin
fn:=GetCurrentDir+ '/PhoneLog.txt';
if FileExists(fn) then
beign
Stream:=TFileStream.Create(fn,fmOpenWrite);
Stream.Seek(0,soFromEnd);
end
else Stream:=TFileStream.Create(fn,fmCreate);
s:=Formatdatetime('yyyy-mm-dd hh:mm:ss',now) + ' ' + LogMsg;
Stream.WriteBuffer(s[1],length(s));
Stream.Free;
end;
// Pascal 的传统方式则是这样:
Procedure Write_log3_1(LogMsg:string);
var
F: TextFile;
fn: string;
begin
fn:=GetCurrentDir+ '/PhoneLog.txt';
AssignFile(F,fn);
if FileExists(fn) then Append(fn)
else Rewrite(F);
Writeln(Formatdatetime('yyyy-mm-dd hh:mm:ss',now) + ' ' + LogMsg);
CloseFile(F);
end;