Hier mal ein übertriebenes Beispiel:
Von einer String Variable in ein TMemoryStream und dann den TMemoryStream in TWriter schreiben:
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
Handle: Integer;
HandleStream: THandleStream;
MemoryStream: TMemoryStream;
Writer: TWriter;
S:
String;
Size: Integer;
begin
Handle := FileCreate('
M:\TEMP\TEST.TXT');
if Handle <> -1
then
begin
HandleStream := THandleStream.Create(
Handle);
MemoryStream := TMemoryStream.Create;
Writer := TWriter.Create(HandleStream, 4096);
try
S := '
TestString';
MemoryStream.WriteBuffer(S[1], Length(S));
MemoryStream.Position := 0;
Size := MemoryStream.Size;
Writer.WriteInteger(Size);
Writer.
Write(MemoryStream.Memory^, Size);
finally
Writer.Free;
MemoryStream.Free;
HandleStream.Free;
FileClose(
Handle);
end;
end;
end;
Und nun von TReader in TMemoryStream und dann in die Variable S:
Delphi-Quellcode:
procedure TForm1.Button2Click(Sender: TObject);
var
Handle: Integer;
HandleStream: THandleStream;
MemoryStream: TMemoryStream;
Reader: TReader;
Size: Integer;
S:
String;
begin
Handle := FileOpen('
M:\TEMP\TEST.TXT', fmOpenRead);
if Handle <> -1
then
begin
HandleStream := THandleStream.Create(
Handle);
MemoryStream := TMemoryStream.Create;
Reader := TReader.Create(HandleStream, 4096);
try
Size := Reader.ReadInteger;
MemoryStream.Size := Size;
MemoryStream.Position := 0;
Reader.
Read(MemoryStream.Memory^, Size);
MemoryStream.Position := 0;
SetLength(S, Size);
MemoryStream.
Read(S[1], Size);
ShowMessage(S);
finally
Reader.Free;
MemoryStream.Free;
HandleStream.Free;
FileClose(
Handle);
end;
end;
end;