Delphi流的操作_文件合并

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    btn2: TButton;
    btn3: TButton;
    mmo1: TMemo;
    mmo2: TMemo;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//合并  use TFileStream
procedure TForm1.btn1Click(Sender: TObject);
var
  fs1,fs2:TFileStream;
begin
  fs1:= TFileStream.Create('a.txt',fmOpenWrite); //主
  fs2:= TFileStream.Create('b.txt',fmOpenRead); //必须为fmOpenRead,如果为fmOpenWrite则报错
  try
    fs1.Seek(0,soFromEnd);//将指针移到主文件的末尾
    fs1.CopyFrom(fs2,0);
  finally
    fs1.Free;
    fs2.free;
  end;

  mmo1.Lines.LoadFromFile('a.txt');
  mmo2.Lines.LoadFromFile('b.txt');
end;

//合并 use TMemoryStream
procedure TForm1.btn3Click(Sender: TObject);
var
  fs1:TFileStream;
  ms: TMemoryStream;
begin
  fs1:= TFileStream.Create('a.txt',fmOpenWrite); //主
  ms:= TMemoryStream.Create;
  try
    ms.LoadFromFile('b.txt');
    fs1.Seek(0,soFromEnd);//将指针移到主文件的末尾
    fs1.CopyFrom(ms,0);
  finally
    fs1.Free;
    ms.free;
  end;

  mmo1.Lines.LoadFromFile('a.txt');
  mmo2.Lines.LoadFromFile('b.txt');
end;

//分割
procedure TForm1.btn2Click(Sender: TObject);
var
  fs1,fs2:TFileStream;
  isize:integer;
  ms:TMemoryStream;
begin
  fs1:= TFileStream.Create('a.txt',fmOpenRead); //主
  fs2:= TFileStream.Create('b.txt',fmOpenRead); //必须为fmOpenRead,如果为fmOpenWrite则报错
  ms:= TMemoryStream.Create;
  try
    isize:= fs2.Size;
    //isize:=18;//18为 文件字节数
    fs1.Seek(-18,soFromEnd);//
    ms.CopyFrom(fs1,18);
    ms.SaveToFile('c.txt');
  finally
    fs1.Free;
    ms.free;
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  mmo1.Lines.LoadFromFile('a.txt');
  mmo2.Lines.LoadFromFile('b.txt');
end;

end.

 

Delphi流的操作_文件合并

上一篇:对比两个文件是否相同的函数


下一篇:ovs-vsctl add-br hangs