我正在操作 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;
uj5u.com熱心網友回復:
一種更簡單的撰寫方法是使用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;
uj5u.com熱心網友回復:
uses System.Classes, System.SysUtils, System.IOUtils;
VAR pathHtml : STRING;
PROCEDURE Test(CONST TXT : STRING);
VAR
FS : TStream;
N : TFileName;
B : TBytes;
BEGIN
N:=TPath.Combine(pathHtml,'filename.txt');
IF NOT TFile.Exists(N) THEN
FS:=TFileStream.Create(N,fmCreate OR fmShareDenyNone)
ELSE BEGIN
FS:=TFileStream.Create(N,fmOpenWrite OR fmShareDenyNone);
FS.Position:=FS.Size
END;
TRY
B:=TEncoding.ANSI.GetBytes(TXT);
FS.WriteBuffer(B,LENGTH(B))
FINALLY
FS.Free
END
END;
實施意見:
- 使用 TPath.Combine 組合目錄和檔案名
- 使用 TEncoding.ANSI.GetBytes 將 UNICODE 字串轉換為 ANSI。您在上面使用的版本有問題,因為 UNICODE 字串的 AnsiString 等效項的長度可能與 UNICODE 字串不同,例如。當您的國家字符在 UNICODE 中作為一對編碼但在 AnsiString 中作為單個位元組進行編碼時,因此您最終可能會(嘗試)寫入比 AnsiString 轉換產生的更多位元組。
- 小心 AnsiString(和 TEncoding.ANSI),因為它取決于運行程式的計算機的語言/區域設定。
如果 Tom 在評論中說的是: Asker 希望將字串放在檔案中的不同行中。 是真的,那么就很簡單了。只需更換線條
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));
和
out1:=out1 #13#10; out2:=out2 #13#10; out3:=out3 #13#10;out4:=out4 #13#10; out5:=out5 #13#10;
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));
將 CR/LF 附加到每行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/357949.html
