当前位置:编程学习 > Delphi >>

TFileStream用法

// 写入中文
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;



摘自 落魄的鸡
补充:软件开发 , Delphi ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,