delphi的TFileStream 内存流


本文整理自网络,侵删。

 一、文件

文本文件是以行为单位进行读、写操作的。文本文件只能单独为读或写而打开,在一个打开的文本文件上同时进行读、写操作是不允许的。

二、定义

FileStream: TFileStream;

三、打开文件

Filestream:= TFileStream.Create(AFileName: string; Mode: Word);

参数AfileName:文件名;参数Mode:文件打开的方式。

Mode由打开模式与共享模式组成,取值见下表: 
分类
 
参数
 
说明
 








 
fmCreate
 
建立文件, 如果指定文件名的文件已经存在,则以写模式打开
 

fmOpenRead
 
只读打开
 

fmOpenWrite
 
以写模式打开文件,写到文件的内容将替换文件以前的内容
 

fmOpenReadWrite
 
读写打开
 








 
fmShareCompat
 
共享模式, 兼容 Dos
 

fmShareExclusive
 
他的应用程序不能打开该文件
 

fmShareDenyWrite
 
其他的应用程序只能以只写方式打开
 

fmShareDenyRead
 
其他的应用程序只能以只读方式打开
 

fmShareDenyNone
 
其他的应用程序可以以任何方式打开文件
 



四、读写文件

function read(var buffer;count:longint):longint; //从文件流当前位置读count字节到缓冲区BUFFER;

function write(const buffer;count:longint):longint; //将缓冲区BUFFER的Count个字节的数据写到文件流当前位置中,覆盖该位置后面的Count个字节的数据;

function seek(offset:longint;origin:word):longint; //设置文件流当前读写指针位置,origin={soFromBeginning,soFromCurrent,soFromEnd}

function copyfrom(source:TStream;count:longint):longint; //从另一文件流中当前位置复制COUNT到当前文件流当前位置;

八、关闭文件

文件的关闭须调用FreeAndNil(FileStream)。



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

一个实例

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

type

TBuffer = array [0..16000]of char

procedure GetMessageFromDir( AFileName: string; var ABuffer: TBuffer);

var

     FileStream: TFileStream;

begin

       Filestream:=TFileStream.Create(AFileName,fmShareExclusive);

       Filestream.Position:=0;

       FileStream.Read(ABuffer,sizeof(ABuffer));

     FreeAndNil(FileStream);

end;

procedure PutMessageToDir(AFileName:string; Astr :string);

var

     FileStream: TFileStream;

     tempBuffer:TBuffer;

begin

     StrPcopy(tempBuffer,Astr);

     Filestream:=TFileStream.Create(AFileName,fmShareExclusive or fmCreate);

     FileStream.Position:=0;

     FileStream.Write(tempBuffer,length(AStr));

     FreeAndNil(FileStream);

end;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

一个实例

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

procedure TForm1.Button1Click(Sender: TObject);

var

getStream,setStream: TFileStream; {声明一个文件流}

getPath,setPath: string;

begin

getPath := 'c:\temp\get.jpg'; {这个文件存在}

setPath := 'c:\temp\set.jpg'; {这个会自动建立}

getStream := TFileStream.Create(getPath, fmOpenRead or fmShareExclusive);

setStream := TFileStream.Create(setPath, fmCreate);

getStream.Position := 0; {流指针移到开始, 复制时从这里开始}

setStream.CopyFrom(getStream, getStream.Size); {Copy 流}

{CopyFrom 的第二个参数是要复制的内容大小; 如果为 0 , 不管指针在什么位置都会复制所有内容}

getStream.Free;

setStream.Free;

end;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

一个实例:读取流中的图片数据,显示图片

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

type

TForm1 = class(TForm)

    img1: TImage;

    procedure FormShow(Sender: TObject);

private

    { Private declarations }

public

    { Public declarations }

    fstream:TFileStream;

end;


var

Form1: TForm1;

filename:string = '.\WeiMir.uib';

implementation


{$R *.dfm}


procedure TForm1.FormShow(Sender: TObject);

var

bmp:Tbitmap;

begin

fstream:=Tfilestream.create(filename,fmOpenRead);

fstream.Seek(0,soFromBeginning);

bmp:=TBitmap.Create;

bmp.LoadFromStream(fstream);

img1.Picture.Bitmap:=bmp;

end;

end.


相关阅读 >>

Delphi 获取当前输入法

Delphi动态创建一个鼠标指针图案

Delphi 取得文件夹及下一级文件夹下的文件列表

Delphi代码,直截注入别的进程,之后直截运行在别的进程中的代码

Delphi fdmemtable内存表操作

Delphi 外挂编写的几个api函数

Delphi 生成全球唯一标识符

Delphi调用javascript解析json

Delphi 提取字符串中所有数字

Delphi 根据文本高度确定richedit高度

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...