首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

怎么判断 没有bom头的 utf-8 文本文件

2013-01-23 
如何判断 没有bom头的 utf-8 文本文件?delphi 判断时 因为没有 文件头标识 会被误认为是ansi 编码 导致 乱

如何判断 没有bom头的 utf-8 文本文件?
delphi 判断时 因为没有 文件头标识 会被误认为是  ansi 编码 导致 乱码


[解决办法]

function UTF8FileBOM(const FileName: string): boolean;
var
  txt: file;
  bytes: array[0..2] of byte;
  amt: integer;
begin
  FileMode := fmOpenRead;
  AssignFile(txt, FileName);
  Reset(txt, 1);
  try
    BlockRead(txt, bytes, 3, amt);
    result := (amt=3) and (bytes[0] = $EF) and (bytes[1] = $BB) and (bytes[2] = $BF);
  finally    
    CloseFile(txt);
  end;
end;

热点排行