问题来源: http://www.cnblogs.com/del/archive/2008/05/29/1005631.html#1212654

首先, 这个问题到 Delphi 2008 时, 可能就不存在了, 因为那时的 TStrings 肯定支持 UniCode; 当前版本是 2007.
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  Stream: TMemoryStream;
  pwc: PWideChar;
begin
  Stream := TMemoryStream.Create;
  Stream.LoadFromFile('c:\temp\UniCode.txt');
  pwc := Stream.Memory;
  //Inc(pwc);  {可能开始字节有个乱码, 这样可以去掉}
  Memo1.Lines.Text := pwc;
  Stream.Free;
end;

end.

做成函数的形式:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//打开 UniCode 格式的文本文件的函数:
function LoadUniCodeTxt(f: string): AnsiString;
var
  Stream: TMemoryStream;
  p: PWideChar;
begin
  Result := '';
  if not FileExists(f) then Exit;
  Stream := TMemoryStream.Create;
  Stream.LoadFromFile(f);
  p := Stream.Memory;
  Inc(p);
  Result := p;
  Stream.Free;
end;

//调用测试:
procedure TForm1.FormCreate(Sender: TObject);
var
  str: string;
begin
  str := LoadUniCodeTxt('c:\temp\UniCode.txt');
  Memo1.Clear;
  Memo1.Lines.Add(str);
end;

end.

posted on 2008-05-29 13:18  万一  阅读(4837)  评论(12编辑  收藏  举报