流的使用分割与合并文件的函数
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} //分割文件的函数 {参数 1 是要分割的文件名; 参数 2 是要风格文件的大小, 单位是 KB} {分割后的文件名扩展名用序号替换} function SplitFile(const FileName: string; Size: Cardinal): Boolean; var fStream: TFileStream; {原始文件} toStream: TMemoryStream; {分文件} p,i: Integer; {p 记录当前指针位置; i 记录这是第几个分的文件} begin Result := False; Size := Size * 1024; {把大小的单位转换为字节} fStream := TFileStream.Create(FileName, fmOpenRead); p := 0; i := 0; toStream := TMemoryStream.Create; while p < fStream.Size do begin toStream.Clear; {清空上次数据} fStream.Position := p; {放好指针位置} if fStream.Size-p < Size then Size := fStream.Size-p; {最后一个时, 有多少算多少} toStream.CopyFrom(fStream, Size); {复制} toStream.SaveToFile(FileName + '.' + IntToStr(i)); {保存} Inc(i); p := p + Size; end; fStream.Free; toStream.Free; Result := True; end; //合并文件, 参数是其中一个分文件名 function MergeFile(const FileName: string): Boolean; var ms: TMemoryStream; {读取分文件} fs: TFileStream; {合并后的文件} path: string; i: Integer; begin path := ChangeFileExt(FileName,''); {去掉序号扩展名} ShowMessage(path); i := 0; ms := TMemoryStream.Create; fs := TFileStream.Create(path, fmCreate); while FileExists(path + '.' + IntToStr(i)) do begin ms.LoadFromFile(path + '.' + IntToStr(i)); fs.CopyFrom(ms, 0); {TFileStream 不需要 SetSize; 但如果用 TMemoryStream 就需要} Inc(i); end; ms.Free; fs.Free; end; //测试分割 procedure TForm1.Button1Click(Sender: TObject); begin SplitFile('c:\temp\test.txt', 10); end; //测试合并 procedure TForm1.Button2Click(Sender: TObject); begin MergeFile('c:\temp\test.txt.0'); end; end.
TFileStream 的读写 合并文件
TFileStream 的读写 合并文件
向TFileStream中写入字符串的问题
sbuff:TString;
S1:Tfilestream;
S1.write(sbuff,length(sbuff))是乱码
S1.write(pChar(sbuff)^,length(sbuff)))是对的~
求解释
S1.write(sbuff,length(sbuff))为什么不可以
还有length(sbuff)和sizaof(sbuff)的区别
解答:
两处错误
1,一定要先为你的string变量t指定长度(分配空间).
2,串变量t并不代表它分配的内存指针.
先SETLENGTH(T,30);
再T.READBUFFER(PCHAR(T)^,30);
不能用T.READBUFFER(T,30);
向TFileStream中写入字符串的问题
sbuff:TString;
S1:Tfilestream;
S1.write(sbuff,length(sbuff))是乱码
S1.write(pChar(sbuff)^,length(sbuff)))是对的~
求解释
S1.write(sbuff,length(sbuff))为什么不可以
还有length(sbuff)和sizaof(sbuff)的区别
解答:
两处错误
1,一定要先为你的string变量t指定长度(分配空间).
2,串变量t并不代表它分配的内存指针.
先SETLENGTH(T,30);
再T.READBUFFER(PCHAR(T)^,30);
//合并文件, 参数是合并后的文件名 function TForm1.MergeFile(const FileName: string): Boolean; const s1 = #13#10#13#10#13#10#13#10; var ms: TMemoryStream; {读取分文件} fs: TFileStream; {合并后的文件} path,spliteStr: string; i: Integer; begin result := False; ms := TMemoryStream.Create; fs := TFileStream.Create(FileName, fmCreate); try for i := 0 to lst1.items.Count - 1 do begin if i = 0then spliteStr := '#' + lst1.items[i] + #13#10#13#10#13#10 else spliteStr := #13#10#13#10#13#10 + '#' + lst1.items[i] + #13#10#13#10#13#10; ms.LoadFromFile(pathtemp + lst1.items[i]); fs.Write(PChar(spliteStr)^,length(spliteStr)); fs.Seek(0,soFromEnd); fs.CopyFrom(ms, 0) end; Result := true; finally ms.Free; fs.Free; end; end; //合并文件, 参数是合并后的文件名 function TForm1.MergeFile(const FileName: string): Boolean; const s1 = #13#10#13#10#13#10#13#10; var ms: TMemoryStream; {读取分文件} fs: TFileStream; {合并后的文件} path,spliteStr: string; i: Integer; begin result := False; ms := TMemoryStream.Create; fs := TFileStream.Create(FileName, fmCreate); try for i := 0 to lst1.items.Count - 1 do begin if i = 0then spliteStr := '#' + lst1.items[i] + #13#10#13#10#13#10 else spliteStr := #13#10#13#10#13#10 + '#' + lst1.items[i] + #13#10#13#10#13#10; ms.LoadFromFile(pathtemp + lst1.items[i]); fs.Write(PChar(spliteStr)^,length(spliteStr)); fs.Seek(0,soFromEnd); fs.CopyFrom(ms, 0) end; Result := true; finally ms.Free; fs.Free; end;
文件分割
var i, WantedFragSize, RealToWrite : Integer; //声明两个文件流对象 //InStream为输入源文件流对象,OutStream为输出切割文件流对象 InStream, OutStream : TFileStream; S : String; begin if Opendialog1.Execute then begin SaveDialog1.Title := '输入你要保存的切割文件名'; If SaveDialog1.Execute then begin WantedFragSize := StrtoInt(Edit1.Text); //定义切割块大小 i := 0; //以只读的方式创建一个输入文件流对象 InStream:=TFileStream.Create( OpenDialog1.FileName,fmOpenRead); Try //如果Instream流的当前指针位置不在末尾,就按照定义的块大小//来读出数据并存成文件 while (InStream.Position < InStream.Size) do Begin s := IntToStr(I); while Length(s) < 3 do s := '0'+s; s := '.'+s; //生成切割文件的扩展名,用三位数字来作为扩展名,方便组合文件 //如果InStream所剩的块大小小于定义的块大小,则将剩下的块数//据存成文件 If InStream.Size - InStream.Position < WantedFragSize then RealToWrite := InStream.Size - InStream.Position else RealToWrite := WantedFragSize; //创建一个输出文件流对象 OutStream:=TFileStream.Create(SaveDialog1.FileName+s,fmCreate); try OutStream.CopyFrom(InStream,RealToWrite); Inc(i); Finally //释放输出文件流对象 OutStream.Free; end; end; Finally InStream.Free; end; end; end;
文件切割组合
//切割文件 procedure TForm1.Button1Click(Sender: TObject); var i, WantedFragSize, RealToWrite : Integer; //声明两个文件流对象 //InStream为输入源文件流对象,OutStream为输出切割文件流对象 InStream, OutStream : TFileStream; S : String; begin if Opendialog1.Execute then begin SaveDialog1.Title := '输入你要保存的切割文件名'; If SaveDialog1.Execute then begin WantedFragSize := StrtoInt(Edit1.Text); //定义切割块大小 i := 0; //以只读的方式创建一个输入文件流对象 InStream:=TFileStream.Create( OpenDialog1.FileName,fmOpenRead); Try //如果Instream流的当前指针位置不在末尾,就按照定义的块大小//来读出数据并存成文件 while (InStream.Position < InStream.Size) do Begin s := IntToStr(I); while Length(s) < 3 do s := '0'+s; s := '.'+s; //生成切割文件的扩展名,用三位数字来作为扩展名,方便组合文件 //如果InStream所剩的块大小小于定义的块大小,则将剩下的块数//据存成文件 If InStream.Size - InStream.Position < WantedFragSize then RealToWrite := InStream.Size - InStream.Position else RealToWrite := WantedFragSize; //创建一个输出文件流对象 OutStream:=TFileStream.Create(SaveDialog1.FileName+s,fmCreate); try OutStream.CopyFrom(InStream,RealToWrite); Inc(i); Finally //释放输出文件流对象 OutStream.Free; end; end; Finally InStream.Free; end; end; end; end; //组合文件 procedure TForm1.Button2Click(Sender: TObject); var i : Integer; InStream, OutStream : TFileStream; SourcePath, FragName, S : String; begin Opendialog1.Title := '请选择切割文件'; if Opendialog1.Execute then begin //得到当前打开的切割文件的路径 SourcePath := ExtractFilePath(Opendialog1.FileName); //得到当前打开的切割文件的文件名 FragName := ExtractFileName(Opendialog1.FileName); SaveDialog1.Title := '请输入你要组合的文件名'; If SaveDialog1.Execute then begin i := 0; s := IntToStr(I); while Length(s) < 3 do s := '0'+s; s := '.'+s; FragName := ChangeFileExt(FragName, s); //如果目录中有以.000为扩展名的文件存在,则创建一个输出文件流//对象 If FileExists(SourcePath+FragName) then begin OutStream:=TFileStream.Create(Savedialog1.FileName,fmCreate); Try //按照以文件扩展名递增的方式组合文件 While FileExists(SourcePath+FragName) do Begin InStream:=TFileStream.Create(SourcePath+FragName,fmOpenRead); Try //将输入文件流的数据写入到输出文件流中 OutStream.CopyFrom(InStream,0); Inc(i); s := IntToStr(I); while Length(s) < 3 do s := '0'+s; s := '.'+s; FragName := ChangeFileExt(FragName, s); finally InStream.Free; end; end; finally OutStream.Free; end; end; end; end; end;