delphi - TFileStream 将数据写入下一行
问题描述
我正在操作 TFileStream 并且我想将数据写入下一行。
以下代码重写了前一个...
我能做些什么来包装并让它在那里写下新的一行?
function TParserSkyecc.GenerateHTMLFile(pathHtml,out1,out2,out3,out4,out5 : String) : Boolean;
var FS: TFileStream;
begin
FS := TFileStream.Create(pathHtml+'\filename.txt',fmCreate or fmOpenWrite or fmShareDenyNone);
try
FS.Seek(0,soFromBeginning);
FS.WriteBuffer(Pointer(AnsiString(out1))^,Length(out1));
FS.WriteBuffer(Pointer(AnsiString(out2))^,Length(out2));
FS.WriteBuffer(Pointer(AnsiString(out3))^,Length(out3));
FS.WriteBuffer(Pointer(AnsiString(out4))^,Length(out4));
FS.WriteBuffer(Pointer(AnsiString(out5))^,Length(out5));
finally
FS.Free;
end;
end;
解决方案
一种更简单的编写方法是使用TStreamWriter
而不是TFileStream
直接使用。在它的构造函数中TStreamWriter
接受 a ,并且有一个方法。例如:TEncoding
WriteLine()
function TParserSkyecc.GenerateHTMLFile(pathHtml,out1,out2,out3,out4,out5 : String) : Boolean;
var
Filename: string;
Writer: TStreamWriter;
begin
Result := False;
try
Filename := IncludeTrailingPathDelimiter(pathHtml) + 'filename.txt'; // or: TPath.Combine(pathHtml, 'filename.txt')
Writer := TStreamWiter.Create(Filename, False, TEncoding.ANSI);
try
Writer.WriteLine(out1);
Writer.WriteLine(out2);
Writer.WriteLine(out3);
Writer.WriteLine(out4);
Writer.WriteLine(out5);
Writer.Flush;
Result := True;
finally
Writer.Free;
end;
except
end;
end;
或者,您可以TStringList
改用:
function TParserSkyecc.GenerateHTMLFile(pathHtml,out1,out2,out3,out4,out5 : String) : Boolean;
var
Filename: string;
SL: TStringList;
begin
Result := False;
try
Filename := IncludeTrailingPathDelimiter(pathHtml) + 'filename.txt'; // or: TPath.Combine(pathHtml, 'filename.txt')
SL := TStringList.Create;
try
SL.Add(out1);
SL.Add(out2);
SL.Add(out3);
SL.Add(out4);
SL.Add(out5);
SL.SaveToFile(Filename, TEncoding.ANSI);
Result := True;
finally
SL.Free;
end;
except
end;
end;
推荐阅读
- c++ - 如何在 LLVM 的 MachineFunctionPass 中正确插入使用 BuildMI() 的机器指令?
- python - Error executing an HTTP request in kaggle
- sql - 如何使用 IF ELSE 解析和更新 T-SQL 中的列值?
- python-3.x - Beautifull Soup 刮网页所有赔率表
- vba - How to search for a folder using text in folder.description?
- python - How to align and add space while writing dataframe tocsv in python
- python - Python将char映射到整数
- python - importing subpackages of sci-kit learn 'Unknown location'
- node.js - Is there a way to append addtiional clients manually to a socket room created by a connected client?
- openssh - '''ssh-keygen''' 中的 -k 标志有什么作用?