Delphi:将多行字符串保存到文件

时间:2014-12-18 18:26:17

标签: delphi tstringlist

晚上好。

我有一个StringList,每条记录都包含一个多行字符串。

MyStringList [0]:

<li>
  Test1
</li>

MyStringList [1]:

<a href="#">
  <b>      
    Test2
  </b>
</a>

如何将这些字符串保存并加载到文件(text,ini,binary,record,savetofile等)?主要问题是我可以将其保存到文本文件中,但在回读时,每行被视为新记录,而第一条记录有3行,第二条记录有5行,依此类推。

对于保存和加载这些字符串的过程,您有什么建议?

1 个答案:

答案 0 :(得分:4)

如果将其保存到.txt文件,则无法正确重新加载,因为您不知道某个给定换行符是否最初嵌入字符串中,还是将两个分隔开来字符串。

如果您将其保存为其他文本格式,例如.ini,则可以根据需要转义/取消换行符,例如:

function Encode(const S: String): String;
begin
  Result := StringReplace(S, '<', '<<', [rfReplaceAll]);
  Result := StringReplace(Result, #13#10, '<CRLF>', [rfReplaceAll]);
  Result := StringReplace(Result, #13, '<CR>', [rfReplaceAll]);
  Result := StringReplace(Result, #10, '<LF>', [rfReplaceAll]);
end;

Ini := TIniFile.Create(...);
try
  Ini.WriteInteger('section', 'count', MyStringList.Count);
  for I := 0 to MyStringList.Count-1 do
    Ini.WriteString('section', IntToStr(I), Encode(MyStringList[I]);
finally
  Ini.Free;
end;

function Decode(const S: String): String;
begin
  Result := StringReplace(S, '<LF>', #10, [rfReplaceAll]);
  Result := StringReplace(Result, '<CR>', #13, [rfReplaceAll]);
  Result := StringReplace(Result, '<CRLF>', #13#10, [rfReplaceAll]);
  Result := StringReplace(Result, '<<', '<', [rfReplaceAll]);

端;

Ini := TIniFile.Create(...);
try
  Count := Ini.ReadInteger('section', 'count', 0);
  for I := 0 to Count-1 do
    MyStringList.Add(Decode(Ini.ReadString('section', IntToStr(I), ''));
finally
  Ini.Free;
end;

如果将其保存为二进制格式,则可以按原样保留换行符,例如:

procedure WriteIntegerToStream(Stream: TStream; Value: Integer);
begin
  Stream.WriteBuffer(Value, SizeOf(Integer));
end;

procedure WriteStringToStream(Stream: TStream; const Value: String);
var
  U: UTF8String;
  Count: Integer;
begin
  U := UTF8String(Value); // or UTF8Encode(Value) prior to D2009
  Count := Length(U);
  WriteIntegerToStream(Stream, Count);
  if Count > 0 then
    Stream.WriteBuffer(PAnsiChar(U)^, Count * SizeOf(AnsiChar));
end;

Strm := TFileStream.Create(..., fmCreate);
try
  WriteIntegerToStream(Stream, MyStringList.Count);
  for I := 0 to MyStringList.Count-1 do
    WriteStringToStream(Stream, MyStringList[I]);
finally
  Stream.Free;
end;

function ReadIntegerFromStream(Stream: TStream): Integer;
begin
  Stream.ReadBuffer(Result, SizeOf(Integer));
end;

function ReadStringFromStream(Stream: TStream): String;
var
  Count: Integer;
  U: UTF8String;
begin
  Count := ReadIntegerFromStream(Stream);
  if Count > 0 then
  begin
    SetLength(U, Count);
    Stream.ReadBuffer(PAnsiChar(U)^, Count * SizeOf(AnsiChar));
    Result := String(U); // or UTF8Decode(U) prior to D2009
  end else
    Result := '';
end;

Stream := TFileStream.Create(..., fmOpenRead or fmShareDenyWrite);
try
  Count := ReadIntegerFromStream(Stream);
  for I := 0 to Count-1 do
    MyStringList.Add(ReadStringFromStream(Stream));
finally
  Stream.Free;
end;
相关问题