const
FILE_SIG = '
MyFileSignature';
BUF_SIZE = 4096;
function SaveStrings(AStream: TStream;
const Strings:
array of TStrings; FileSig:
string = '
'): Boolean;
var
Writer: TWriter;
i: Integer;
begin
Result := False;
Writer := TWriter.Create(aStream, BUF_SIZE);
try
if FileSig <> '
'
then
begin
Writer.WriteSignature;
Writer.WriteIdent(FileSig);
end;
Writer.WriteListBegin;
for i := Low(Strings)
to High(Strings)
do
begin
Writer.WriteString(Strings[i].Text);
end;
Writer.WriteListEnd;
Result := True;
finally
Writer.Free;
end;
end;
function LoadStrings(AStream: TStream;
const Strings:
array of TStrings; FileSig:
string = '
'): Boolean;
var
Reader: TReader;
i: Integer;
begin
Result := False;
Reader := TReader.Create(aStream, BUF_SIZE);
try
if FileSig <> '
'
then
begin
Reader.ReadSignature;
if Reader.ReadIdent <> FileSig
then
raise Exception.Create('
ungültige Datei...');
end;
i := Low(Strings);
Reader.ReadListBegin;
while not Reader.EndOfList
do
begin
if i <= High(Strings)
then
Strings[i].Text := Reader.ReadString;
Inc(i);
end;
Reader.ReadListEnd;
Result := True;
finally
Reader.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
fs: TFileStream;
begin
fs := TFileStream.Create('
C:\MyStrings.txt', fmCreate);
try
SaveStrings(fs, [Memo1.Lines, Memo2.Lines, ListBox1.Items], FILE_SIG);
finally
fs.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
fs: TFileStream;
begin
fs := TFileStream.Create('
C:\MyStrings.txt', fmOpenRead);
try
LoadStrings(fs, [Memo1.Lines, Memo2.Lines, ListBox1.Items], FILE_SIG);
finally
fs.Free;
end;
end;