function Tform1.Test; var fs:TFileStream; fs2:TFileStream; fs3:TFileStream; str:string; ms:TMemoryStream; ms2:TMemoryStream; sr:TStream; begin //TFileStream创建文本文件 str:='文件操作'; fs:=TFileStream.Create('c:\1.txt',fmCreate); fs.WriteBuffer((PChar(str))^,Length(str)); fs.Free; //TFileStream读取文本文件 fs:=TFileStream.Create('c:\1.txt',fmOpenRead); SetLength(str,fs.size); fs.ReadBuffer((PChar(str))^,fs.Size); ShowMessage(str); fs.Free; //TFileStream读取文本文件 ms:=TMemoryStream.Create; ms.LoadFromFile('c:\1.txt'); SetLength(str,ms.size); ms.ReadBuffer((PChar(str))^,ms.size); ms.Free; ShowMessage(str); //TMemoryStream创建文本文件 str:='文件操作'; ms:=TMemoryStream.Create; ms.WriteBuffer((PChar(str))^,Length(str)); ms.SaveToFile('c:\2.txt'); ms.Free; //TMemoryStream文件合并 ms:=TMemoryStream.Create; ms.LoadFromFile('c:\1.txt'); ms.Position:=ms.Size;//移动文件指针 ms2:=TMemoryStream.Create; ms2.LoadFromFile('c:\2.txt'); ms.CopyFrom(ms2,0); ms.SaveToFile('c:\3.txt'); ms.Free; ms2.Free; end;
时间: 2024-10-13 02:30:42