也可以用判断文本文件的方式进行。 function GetTextType(const FileName: string): String; var w: Word; b: Byte; begin with TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone) do try Read(w,2); w:=WordLoHiExchange(w);//因为是以Word数据类型读取,故高低字节互换 if w = TextFormatFlag[tfUnicode] then Result := 'tfUnicode' else if w = TextFormatFlag[tfUnicodeBigEndian] then Result:= 'tfUnicodeBigEndian' else if w = TextFormatFlag[tfUtf8] then Result := 'tfUtf8' else Result := 'tfANSI'; finally Free; end; end;
utf/unicode到ansi,有专门的函数:以UTF8Decode查帮助,就能得到一整套了
function GetTextType(const FileName: string): String;
var
w: Word;
b: Byte;
begin
with TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone) do
try
Read(w,2);
w:=WordLoHiExchange(w);//因为是以Word数据类型读取,故高低字节互换
if w = TextFormatFlag[tfUnicode] then
Result := 'tfUnicode'
else if w = TextFormatFlag[tfUnicodeBigEndian] then
Result:= 'tfUnicodeBigEndian'
else if w = TextFormatFlag[tfUtf8] then
Result := 'tfUtf8'
else
Result := 'tfANSI';
finally
Free;
end;
end;