{ TFile }constructor TFile.Create; begin inherited; FStream := TMemoryStream.Create; end;destructor TFile.Destroy; begin FreeAndNil(FStream); inherited; end;procedure TFile.DoLoadFile(FileName: String); begin FStream.LoadFromFile(FileName); end;procedure TFile.DoSaveFile(FileName: String); begin FStream.SaveToFile(FileName); end;procedure TFile.LoadFromFile(FileName: String); begin FName := FileName; DoLoadFile(FileName); end;procedure TFile.SaveToFile(FileName: String); begin if FileName = '' then begin FName := FileName; DoSaveFile(FName); end else DoSaveFile(FileName); end;{ TTxtFile }constructor TTxtFile.Create; begin inherited; FStrList := TStringList.Create; end;destructor TTxtFile.Destroy; begin FreeAndNil(FStrList); inherited; end;procedure TTxtFile.DoLoadFile(FileName: String); begin inherited; FStrList.LoadFromStream(FStream); end;procedure TTxtFile.DoSaveFile(FileName: String); begin FStrList.SaveToStream(FStream); inherited; end;function TTxtFile.GetFileContent: String; begin Result := FStrList.Text; end;procedure TTxtFile.SetFileContent(const Value: String); begin if Value <> FStrList.Text then FStrList.Text := Value; end;{ TFileList }constructor TFileList.Create; begin inherited; FItemClass := TFile; FList := FList.Create; FList.Capacity := 256; end;destructor TFileList.Destroy; var I: Integer; begin for I := 0 to FList.Count - 1 do TObject(FList[I]).Free; FreeAndNil(FList); inherited; end;function TFileList.GetItems(Index: Integer): TFile; begin Result := nil; if (Index >= 0) and (Index < FList.Count - 1) then Result := FList[Index]; end;procedure TFileList.LoadFile(FileName: String); var AFile: TObject; begin AFile := FItemClass.NewInstance; AFile := AFile.Create; TFile(AFile).LoadFromFile(FileName); FList.Add(AFile); end;procedure TFileList.SaveAllFile; var I: Integer; TempFile: TFile; begin for I := 0 to FList.Count - 1 do begin TempFile := FList[I]; TempFile.SaveToFile; end; end;procedure TFileList.SetItems(Index: Integer; const Value: TFile); begin if (Index >= 0) and (Index < FList.Count - 1) then begin TObject(FList[Index]).Free; FList[Index] := Value; end else FList.Add(Value); end;{ TTxtFileList }constructor TTxtFileList.Create; begin inherited; FItemClass := TTxtFile; end;function TTxtFileList.GetItems(Index: Integer): TTxtFile; begin Result := nil; Inherited GetItems(Index); end;procedure TTxtFileList.SetItems(Index: Integer; const Value: TTxtFile); begin Inherited SetItems(Index, Value); end;
//简单实现,献丑 type TTxtFileList=Class(TStringList) private function Get(index1,index2:integer):String;virtual; public Constructor Create; Function LoadFile(FileName:Array of String):Boolean; destructor destroy;override; property Files[index1,index2:integer]: String read Get; default; end;Constructor TTxtFileList.Create; begin inherited; end; destructor TTxtFileList.destroy; var i:integer; begin for i:=0 to Count-1 do TStringList(Objects[i]).Free; inherited; end; function TTxtFileList.Get(index1,index2:integer):String; begin if (Index1<0) or (Index1>=Count) then Raise exception.Create('index1 out of range!'); if (Index2<0) or (Index2>=TStrings(Objects[Index1]).Count) then Raise exception.Create('index2 out of range!'); Result:=TStrings(Objects[Index1])[index2];end;function TTxtFileList.LoadFile(FileName:Array of String):Boolean; var i:integer; begin ReSult:=false; for i:=Low(FileName) to High(FileName) do begin AddObject(FileName[i],TStringList.Create); try TStringList(objects[i]).LoadfromFile(FileName[i]); except ShowMessage(Filename[i]+'not found!'); exit; end; end; Result:=True; end;procedure TForm1.Button1Click(Sender: TObject); var aa:TTxtFileList; begin aa:=TTxtFileList.Create; aa.LoadFile(['c:\temp\testa_d.pas','c:\temp\testa.pas']) ; edit1.Text:=aa[0,0]; aa.Free;end;
哈哈,高手指点下问题解决了,其实很简单,设为n个文件var mydata:array of array of stringbegin setlength(mydata,n); for k:=0 to high(mydata) do begin setlength(mydata[k],x);//x为第k+1个文件行数 //这里写代码,将第k+1个文件的数据逐行读入mydata[k] mydata[k,0]:=... mydata[k,1]:=... ... mydata[k,x-1]:=... end;end;
private
FName: String;
FStream: TMemoryStream;
protected
procedure DoLoadFile(FileName: String); virtual;
procedure DoSaveFile(FileName: String); virtual;
public
constructor Create; virtual;
destructor Destroy; override;
procedure LoadFromFile(FileName: String);
procedure SaveToFile(FileName: String = '');
property FileName: String read FName write FName;
end; TTxtFile = class(TFile)
private
FStrList: TStringList;
function GetFileContent: String;
procedure SetFileContent(const Value: String);
protected
procedure DoLoadFile(FileName: String); override;
procedure DoSaveFile(FileName: String); override;
public
constructor Create; override;
destructor Destroy; override;
property FileContent: String read GetFileContent write SetFileContent;
end; TFileList = class
private
FList: TList;
FItemClass: TClass;
function GetItems(Index: Integer): TFile;
procedure SetItems(Index: Integer; const Value: TFile);
public
constructor Create; virtual;
destructor Destroy; override;
procedure LoadFile(FileName: String);
procedure SaveAllFile;
property Items[Index: Integer] :TFile read GetItems write SetItems;
end; TTxtFileList = class(TFileList)
private
function GetItems(Index: Integer): TTxtFile;
procedure SetItems(Index: Integer; const Value: TTxtFile);
public
constructor Create; override;
property Items[Index: Integer] :TTxtFile read GetItems write SetItems;
end;
begin
inherited;
FStream := TMemoryStream.Create;
end;destructor TFile.Destroy;
begin
FreeAndNil(FStream);
inherited;
end;procedure TFile.DoLoadFile(FileName: String);
begin
FStream.LoadFromFile(FileName);
end;procedure TFile.DoSaveFile(FileName: String);
begin
FStream.SaveToFile(FileName);
end;procedure TFile.LoadFromFile(FileName: String);
begin
FName := FileName;
DoLoadFile(FileName);
end;procedure TFile.SaveToFile(FileName: String);
begin
if FileName = '' then
begin
FName := FileName;
DoSaveFile(FName);
end
else
DoSaveFile(FileName);
end;{ TTxtFile }constructor TTxtFile.Create;
begin
inherited;
FStrList := TStringList.Create;
end;destructor TTxtFile.Destroy;
begin
FreeAndNil(FStrList);
inherited;
end;procedure TTxtFile.DoLoadFile(FileName: String);
begin
inherited;
FStrList.LoadFromStream(FStream);
end;procedure TTxtFile.DoSaveFile(FileName: String);
begin
FStrList.SaveToStream(FStream);
inherited;
end;function TTxtFile.GetFileContent: String;
begin
Result := FStrList.Text;
end;procedure TTxtFile.SetFileContent(const Value: String);
begin
if Value <> FStrList.Text then
FStrList.Text := Value;
end;{ TFileList }constructor TFileList.Create;
begin
inherited;
FItemClass := TFile;
FList := FList.Create;
FList.Capacity := 256;
end;destructor TFileList.Destroy;
var
I: Integer;
begin
for I := 0 to FList.Count - 1 do
TObject(FList[I]).Free;
FreeAndNil(FList);
inherited;
end;function TFileList.GetItems(Index: Integer): TFile;
begin
Result := nil;
if (Index >= 0) and (Index < FList.Count - 1) then
Result := FList[Index];
end;procedure TFileList.LoadFile(FileName: String);
var
AFile: TObject;
begin
AFile := FItemClass.NewInstance;
AFile := AFile.Create;
TFile(AFile).LoadFromFile(FileName);
FList.Add(AFile);
end;procedure TFileList.SaveAllFile;
var
I: Integer;
TempFile: TFile;
begin
for I := 0 to FList.Count - 1 do
begin
TempFile := FList[I];
TempFile.SaveToFile;
end;
end;procedure TFileList.SetItems(Index: Integer; const Value: TFile);
begin
if (Index >= 0) and (Index < FList.Count - 1) then
begin
TObject(FList[Index]).Free;
FList[Index] := Value;
end else
FList.Add(Value);
end;{ TTxtFileList }constructor TTxtFileList.Create;
begin
inherited;
FItemClass := TTxtFile;
end;function TTxtFileList.GetItems(Index: Integer): TTxtFile;
begin
Result := nil;
Inherited GetItems(Index);
end;procedure TTxtFileList.SetItems(Index: Integer; const Value: TTxtFile);
begin
Inherited SetItems(Index, Value);
end;
我想 就是利用TList容器来存取文件,可以简化一些。
改变长度:SetLength这样的确够简单
^_^
然后用 MapViewOfFile, 把文件句柄印射成一个指针来操作
记得要在用完后 UnmapViewOfFile 和 CloseHandle要不然就干脆用 TStringList, 这个是读入内存的,不过跟你的方法没什么区别
StringList.LoadFromFile(...)哈
StringList[0] 是第一行
type
TTxtFileList=Class(TStringList)
private
function Get(index1,index2:integer):String;virtual;
public
Constructor Create;
Function LoadFile(FileName:Array of String):Boolean;
destructor destroy;override;
property Files[index1,index2:integer]: String read Get; default;
end;Constructor TTxtFileList.Create;
begin
inherited;
end;
destructor TTxtFileList.destroy;
var i:integer;
begin
for i:=0 to Count-1 do
TStringList(Objects[i]).Free;
inherited;
end;
function TTxtFileList.Get(index1,index2:integer):String;
begin
if (Index1<0) or (Index1>=Count) then
Raise exception.Create('index1 out of range!');
if (Index2<0) or (Index2>=TStrings(Objects[Index1]).Count) then
Raise exception.Create('index2 out of range!');
Result:=TStrings(Objects[Index1])[index2];end;function TTxtFileList.LoadFile(FileName:Array of String):Boolean;
var i:integer;
begin
ReSult:=false;
for i:=Low(FileName) to High(FileName) do
begin
AddObject(FileName[i],TStringList.Create);
try
TStringList(objects[i]).LoadfromFile(FileName[i]);
except
ShowMessage(Filename[i]+'not found!');
exit;
end;
end;
Result:=True;
end;procedure TForm1.Button1Click(Sender: TObject);
var aa:TTxtFileList;
begin
aa:=TTxtFileList.Create; aa.LoadFile(['c:\temp\testa_d.pas','c:\temp\testa.pas']) ;
edit1.Text:=aa[0,0]; aa.Free;end;
否则用TStringlist就可以搞定了,而且该类有许多有用的操作如indexof等等
mydata:array of array of stringbegin
setlength(mydata,n);
for k:=0 to high(mydata) do
begin
setlength(mydata[k],x);//x为第k+1个文件行数
//这里写代码,将第k+1个文件的数据逐行读入mydata[k]
mydata[k,0]:=...
mydata[k,1]:=...
...
mydata[k,x-1]:=...
end;end;