流与文件读写

编写与测试环境:winxp sp3 + delphi xe


下载地址


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Menus, StdCtrls, Dialogs, ExtCtrls, pngimage, jpeg, GIFImg;

type
  recData = record
    Name: array [0..9] of AnsiChar;
    Age: Byte;
    BirthDay: TDateTime;
    Tel: array [0..15] of AnsiChar;
    PicSize: Integer;
    PicExt: array [0..5] of AnsiChar;
    Pic: array [0..50 * 1024 - 1] of Byte;      //限50K以内图片
  end;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FFileStream: TFileStream;
    procedure InitData(Data: recData; Name: AnsiString; Age: Byte; Birthday:
      TDateTime; Tel, PicDir: string);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  Data: recData;
begin
  if FFileStream <> nil then FFileStream.Free;
  FFileStream := TFileStream.Create('C:\test.dat', fmCreate);

  try
    InitData(Data, 'AAAAA', 1, StrToDateTime('1999-12-01'), '1111', 'C:\testPic\A.jpg');
    InitData(Data, 'BBBBB', 2, StrToDateTime('1999-12-02'), '2222', 'C:\testPic\B.bmp');
    InitData(Data, 'CCCCC', 3, StrToDateTime('1999-12-03'), '3333', 'C:\testPic\C.png');
    InitData(Data, 'DDDDD', 4, StrToDateTime('1999-12-04'), '4444', 'C:\testPic\D.jpeg');
    InitData(Data, 'EEEEE', 5, StrToDateTime('1999-12-05'), '5555', 'C:\testPic\E.png');
    InitData(Data, 'FFFFF', 6, StrToDateTime('1999-12-06'), '6666', 'C:\testPic\F.ico');
    InitData(Data, 'GGGGG', 7, StrToDateTime('1999-12-07'), '7777', 'C:\testPic\G.gif');

    ShowMessage('写入成功');
  finally
    FFileStream.Free;
    FFileStream := nil;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  Data: recData;
  PicStream: TMemoryStream;
  G: TGraphic;
begin
  if FFileStream = nil then
    FFileStream := TFileStream.Create('c:\test.dat', fmOpenRead);

  if FFileStream.Size > FFileStream.Position then
  begin
    FFileStream.Read(Data, SizeOf(recData));
    Edit1.Text := string(Data.Name);
    Edit2.Text := IntToStr(Data.Age);
    Edit3.Text := DateTimeToStr(Data.BirthDay);
    Edit4.Text := string(Data.Tel);
    PicStream := TMemoryStream.Create;
    try
      PicStream.WriteBuffer(Data.Pic, Data.PicSize);
      PicStream.Position := 0;

      if Data.PicExt = '.png' then G := TPngImage.Create
      else if Data.PicExt = '.bmp' then G := TBitmap.Create
      else if Data.PicExt = '.jpg' then G := TJPEGImage.Create
      else if Data.PicExt = '.jpeg' then G := TJPEGImage.Create
      else if Data.PicExt = '.ico' then G := TIcon.Create
      else if Data.PicExt = '.gif' then
      begin
        G := TGIFImage.Create;
        TGIFImage(G).Animate := True;
      end
      else
      begin
        Image1.Picture.Graphic := nil;
        Exit;
      end;
      try
        G.LoadFromStream(PicStream);
        Image1.Picture.Assign(G);
      finally
        G.Free;
      end;
    finally
      PicStream.Free;
    end;

    if FFileStream.Size = FFileStream.Position then
      FFileStream.Position := 0;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Position := poScreenCenter;

  Button1.Caption := '写入文件';
  Button2.Caption := '读取下一个';

  Application.Icon.Handle := 0;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if FFileStream <> nil then
    FFileStream.Free;
end;

procedure TForm1.InitData(Data: recData; Name: AnsiString; Age: Byte;
  Birthday: TDateTime; Tel, PicDir: string);
var
  PicStream: TFileStream;
  tmp: AnsiString;
begin
  ZeroMemory(@Data, SizeOf(recData));
  StrCopy(Data.Name, PAnsiChar(Name));
  Data.Age := Age;
  Data.BirthDay := Birthday;

  tmp := AnsiString(LowerCase(ExtractFileExt(PicDir)));
  CopyMemory(@Data.PicExt, PAnsiChar(tmp), Length(tmp));
  tmp := AnsiString(Tel);
  CopyMemory(@Data.Tel, PAnsiChar(tmp), Length(tmp));

  PicStream := TFileStream.Create(PicDir, fmOpenRead);
  try
    Data.PicSize := PicStream.Size;
    PicStream.Read(Data.Pic, Data.PicSize);
  finally
    PicStream.Free;
  end;

  FFileStream.Write(Data, SizeOf(recData));
end;

initialization
  System.ReportMemoryLeaksOnShutdown := True;

end.



object Form1: TForm1
  Left = 463
  Top = 225
  Caption = 'Form1'
  ClientHeight = 286
  ClientWidth = 531
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object Image1: TImage
    Left = 232
    Top = 64
    Width = 273
    Height = 181
    Center = True
  end
  object Button1: TButton
    Left = 32
    Top = 39
    Width = 75
    Height = 25
    Caption = #20889#20837#25991#20214
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 126
    Top = 39
    Width = 75
    Height = 25
    Caption = #35835#21462#19979#19968#20010
    TabOrder = 1
    OnClick = Button2Click
  end
  object Edit1: TEdit
    Left = 32
    Top = 80
    Width = 169
    Height = 21
    TabOrder = 2
    Text = 'Edit1'
  end
  object Edit2: TEdit
    Left = 32
    Top = 128
    Width = 169
    Height = 21
    TabOrder = 3
    Text = 'Edit2'
  end
  object Edit3: TEdit
    Left = 32
    Top = 176
    Width = 169
    Height = 21
    TabOrder = 4
    Text = 'Edit3'
  end
  object Edit4: TEdit
    Left = 32
    Top = 224
    Width = 169
    Height = 21
    TabOrder = 5
    Text = 'Edit4'
  end
end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值