|
delphi 文件的读取(二进制文件和文本文件)
- procedure TForm_main.Button5Click(Sender: TObject);
- var
- myStream:TFileStream;
- myFileSize:Integer;
- myTextFile:TextFile;
- text,data,fileStr:string;
- fileData:TStringList;
- memory:TMemoryStream;
- stringStream: TStringStream;
- buffer:PChar;
- resource:TResourceStream;
- jj:TCustomMemoryStream;
- MyWrite:TWriter;
- MyReader:TReader;
- begin
- //解析文件 普通读取
- //myStream:=TFileStream.Create('');
- AssignFile(myTextFile,Edit1.Text);
- Reset(myTextFile);
- while not Eof(myTextFile) do
- begin
- readln(myTextFile,text);
- data:=data+text;
- //ShowMessage(text);
- end;
- CloseFile(myTextFile);
- //ShowMessage(data);
- //TStringList 读取文件
- fileData:=TStringList.Create;
- fileData.LoadFromFile(Edit1.Text);
- //ShowMessage('行数:'+inttostr(fileData.Count));
- //Application.MessageBox(PWideChar(fileData.Text),'系统提示',0);
- fileData.Free;
- //TFileStream二进制读取
- myStream:=TFileStream.Create(Edit1.Text,fmShareDenyRead);
- myStream.Position:=0;
- //myStream.Seek(0);
- GetMem(buffer,myStream.Size);
- //SetLength(buffer,myStream.Size);
- myStream.Read(buffer^,myStream.Size);
- ShowMessage(buffer);
- fileStr:=Pchar(@buffer[0]);
- ShowMessage(fileStr);
- // TReader对应TWriter
- //MyReader:=TReader.Create(myStream,myStream.Size);
- // filestr:= MyReader.ReadString;
- // ShowMessage(fileStr);
- FreeAndNil(myStream);
- //
- memory:=TMemoryStream.Create;
- memory.LoadFromFile(Edit1.Text);
- //memory.SaveToFile('test.txt');
- memory.ReadBuffer(buffer^,memory.Size);
- ShowMessage(memory.ToString);
- ShowMessage(buffer);
-
- //TStringStream读取文件
- stringStream:=TStringStream.Create;
- stringStream.LoadFromFile(Edit1.Text);
- ShowMessage(stringStream.DataString);
- stringStream.Free;
-
- end;
复制代码
|
|