TFileStream用法

页面导航:首页 > 软件编程 > Delphi > TFileStream用法

TFileStream用法

来源: 作者: 时间:2016-01-18 15:41 【

// 写入中文procedure TForm1.btn1Click(Sender: TObject);var Stream : TFileStream; fn : string; fname : string; Size : Integer; Buffer : TBytes; Encoding : TEncoding...
// 写入中文
procedure TForm1.btn1Click(Sender: TObject);
var
    Stream : TFileStream;
  fn : string;
  fname : string;
  Size : Integer;
  Buffer : TBytes;
  Encoding : TEncoding;
begin
  fn := ExtractFilePath(Application.ExeName) + 'config.dat';
  try
    Stream := TFileStream.Create(fn, fmCreate);
    Stream.Position := 0;
    fname := '这是一个测试';
    Encoding := TEncoding.Unicode;
    Buffer := Encoding.GetBytes(fname);
    Size := Length(Buffer);
    Stream.WriteBuffer(Pointer(Buffer)^, Size);
  finally
  Stream.Free;
  end;
end;
// 读取中文
procedure TForm1.btn2Click(Sender: TObject);
var
  Stream : TFileStream;
  fn : string;
  fname : string;
  Size : Integer;
  Buffer : TBytes;
  Encoding : TEncoding;
begin
  fn := ExtractFilePath(Application.ExeName) + 'config.dat';
  try
    Stream := TFileStream.Create(fn, fmOpenRead);
    Stream.Position := 0;
    // fname := '这是一个测试';
    // Encoding := TEncoding.Unicode;
    // Buffer := Encoding.GetBytes(fname);
    // Size := Length(Buffer);
    Size := 12;
    SetLength(Buffer, Size);
    Stream.ReadBuffer(Pointer(Buffer)^, Size);
    Size := TEncoding.GetBufferEncoding(Buffer, Encoding);
    // fname := Encoding.GetString(Buffer, Size, Length(Buffer) - Size);
    fname := WideStringOf(Buffer);
  finally
    Stream.Free;
  end;
end;



摘自 落魄的鸡
Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<