Delphi 取得TXT文件编码

2022-12-03 16:24:15 作者:admin

本文整理自网络,侵删。

 type  TTextFormat=(tfAnsi,tfUnicode,tfUnicodeBigEndian,tfUtf8);const  TextFormatFlag:array[tfAnsi..tfUtf8] of word=($0000,$FFFE,$FEFF,$EFBB); 
function WordLoHiExchange(w:Word):Word;register;asm  XCHG AL, AHend;
{ TextFormat返回文本编码类型,sText未经处理的文本 }procedure ReadTextFile(const FileName: string;  var TextFormat: TTextFormat; var sText: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      TextFormat:= tfUnicode    else if w = TextFormatFlag[tfUnicodeBigEndian] then      TextFormat:= tfUnicodeBigEndian    else if w = TextFormatFlag[tfUtf8] then    begin      Read(b,1);//这里要注意一下,UFT-8必须要跳过三个字节。      TextFormat:=tfUtf8;    end else    begin      TextFormat:=tfANSI;      Position:=0;    end;    SetLength(sText,Size-Position);    ReadBuffer(sText[1],Size-Position);  finally    Free;  end;end;

相关阅读 >>

Delphi 去掉mdi窗口的滚动条

Delphi 用 directshow 获取本机的视频摄像设备列表

Delphi mediaplayer android 下播放 mp4 的问题

Delphi 获取超gb大文件大小

Delphi中的unicode转换

Delphi实时监视应用程序内存使用量

Delphi如何计算一个文件目录的大小

Delphi twebbrowser get html source after ajax load

Delphi操作多显示器

Delphi 让程序只运行1次

更多相关阅读请进入《Delphi》频道 >>



在线咨询 拨打电话