unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject); //先创建一个测试文件
var
  list:TStringList;
begin
  list:=TStringList.Create;
  list.Add('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
  list.SaveToFile('f:\temp\test.txt');
  list.Free;

  Memo1.Lines.LoadFromFile('f:\temp\test.txt');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ms:TMemoryStream;
  c:Char;
  s1,s2:string;
begin
  ms:=TMemoryStream.Create;
  ms.LoadFromFile('f:\temp\test.txt'); {读入内存流}

  s1:='';
  s2:='';
  ms.Position:=0;                   {指针到开始}
  while ms.Position < ms.Size do    {循环读出}
  begin
    ms.Read(c,1);                   {每读出一个字节, 指针会自动移到新的位置}
    s1:=s1 + c + ' ';               {读出来的字符加上空格后追加到s1变量里}
    s2:=s2 + IntToHex(Byte(c),2) + ' '; {用两位数的十六进制记录}
  end;
  Memo1.Lines.Text:=s1;
  Memo2.Lines.Text:=s2;

  ms.Free;        {释放内存流对象 }
end;

end.

posted on 2010-11-16 15:32  巅枫  阅读(727)  评论(0)    收藏  举报