程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> TFileStream用法

TFileStream用法

編輯:Delphi

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



摘自 落魄的雞

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved